在jdk中,有很多地方可以检查关于array.eg的参数。
/*..........
*
* @throws IllegalArgumentException
* If <tt>offset</tt> is negative or greater than
* <tt>buf.length</tt>, or if <tt>length</tt> is negative, or if
* the sum of these two values is negative.
*
* @param buf Input buffer (not copied)
* @param offset Offset of the first char to read
* @param length Number of chars to read
*/
public CharArrayReader(char buf[], int offset, int length) {
if ((offset < 0) || (offset > buf.length) || (length < 0) ||
//$ offset+length
(**(offset + length) < 0)**) {
throw new IllegalArgumentException();
}
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
this.markedPos = offset;
}为什么需要“(offset + length) <0”?
发布于 2012-12-04 09:07:15
在Java语言中,int是有符号的,所以当两个正int相加时,可能会产生负值。这就是所谓的wrap wrap或Integer Overflow
发布于 2012-12-04 09:09:24
我相信它正在检查溢出。
int的范围是-2,147,483,648到2,147,483,647。
从代码中你可以看到,有一些地方我们需要偏移+长度。如果offset + length大于2,147,483,647,则会出现问题,(offset + length) < 0)检查这种情况。
https://stackoverflow.com/questions/13694505
复制相似问题