-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
beta
-
sparc
-
solaris_2.6
Name: acR10002 Date: 12/03/2000
The java.awt.Rectangle.inside(int x, int y) method is implemented to return :
(x >= this.x) && ((x - this.x) < this.width) &&
(y >= this.y) && ((y - this.y) < this.height);
^^^^^^^^^^^^
overflow condition depends on the input values
This code may return invalid result if the boundary point is tested against
a valid rectangle. Please consider the following test for example:
------------------ Test.java ------------------
import java.awt.Rectangle;
public class Test {
public static void main(String args[]) {
Rectangle r = new Rectangle(-1,-1,1,1);
System.out.println(r.inside(Integer.MAX_VALUE,Integer.MAX_VALUE));
}
}
-----------------------------------------------
An output form this test will be "true" for all JDK versions, though it's
obvious that the point doesn't belong to the rectangle. Regarding this test,
the inside() method will return the correct result if implemented as following:
(x >= this.x) && (x < this.x + this.width) &&
(y >= this.y) && (y < this.y + this.height);
^^^^^^^^^^^^^^^^^^^^^
overflow condition depends on in the internal object state only
Though the both of the described above algorithms will fail under certain overflow
conditions, the second one is more preferable. In the second case, the correctness
of the result returned depends only on the Rectangle itself and doesn't depend on
the input values for inside() method.
The same is also applied to contains(int,int) method as long as it uses inside() method
to do the actual calculations.
======================================================================
- relates to
-
JDK-6423143 Rectangle methods should protect against overflow conditions where possible
- Resolved