java - How do you make a defensive copy in a constructor if the input parameter is invalid -
In Josh Bloch's excellent book under Item 39 he says:
"[ D] Copies are made first before examining the validity of critical parameters, and validity is checked on copies compared to the original. "
Example given below:
Public period (date start, end of date) {this.start = new date (start.getTime ()); This.end = new date (end.getTime ()); If (this.start.compareTo (this.end)> 0) new illegal throw exceptions ("..."); }}
The problem with checking the validity after a defensive copy is that an invalid parameter may be caused due to a failed copy, for example, the above class is a
NullPointerException If you enter
null for
start or
end .
If I check the validity before the defensive copy, then I am sensitive to the time-to-trial / time-of-use attack, which is the first to say the bloch due to the defensive copy. P>
My question is what is a way around it? I do not believe that I am the first person to see this problem in a well-read book (although for the book Ereta does not say anything about it), so maybe I've forgotten something.
As other people said, before you copy the parameter
null Check it out.
If I check the validity before the defensive copy, then I am sensitive to the attack of check-time / time-usage which is to inform Bloch the first defensive copy.
No, a hacker will not be able to convert the context of a real example into a blank reference context, or vice versa. The copy is used to avoid changes in the internal position of arguments from another thread.
Comments
Post a Comment