Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4394579

awt.Rectangle.intersects()/contains() behave incorrectly at boundary conditions

XMLWordPrintable

    • 2d
    • beta
    • sparc
    • solaris_2.5.1, solaris_2.6, solaris_7



      Name: acR10002 Date: 12/03/2000


      The Rectangle.intersects(Rectangle) method will behave incorrectly if rectangle
      object has been created in (or put into) the state then it's limits are out of integer
      range, i.e. x + width > Integer.MAX_VALUE or y + height > Integer.MAX_VALUE. Below
      is one or more examples :

      ------ Test1.java ------------

      import java.awt.Rectangle;

      public class Test1 {
          public static void main(String args[]) {
      Rectangle r = new Rectangle(1, 0, Integer.MAX_VALUE, 1);
      if (r.intersects(r)) {
      System.out.println("OKAY: rectangle intersects itself");
      } else {
      System.out.println("FAILURE: rectangle doesn't intersect itself"); }
          }
      }

      ------------------------------
      --> java Test1
      FAILURE: rectangle doesn't intersect itself

      This happens due to an int overflow during calculations. The same problem may happen to
      contains(int,int) and contains(int,int,int,int) methods, for example:

      ------ Test2.java ------------
      import java.awt.Rectangle;

      public class Test2 {
          public static void main(String args[]) {
      Rectangle r = new Rectangle(1, 0, Integer.MAX_VALUE, 1);
      if (r.contains(1, 0, 1, 1)) {
      System.out.println("OKAY: rectangle contains smaller one, as expected");
      } else {
      System.out.println("FAILURE: rectangle doesn't contain smaller one");
      }
          }
      }
      ------------------------------
      --> java -classpath . Test2
      FAILURE: rectangle doesn't contain smaller one

      There are two possible solutions for this problem:
      1. Specification should state explicitly that behavior for contains() and intersects() methods
      is undefined if the limits of this rectangle or rectangle passed as an argument are out
      of integer range (the similar clarification has been already made for negative width/height
      rectangles).
      2. Implementation should be changed to use long, for example, during internal calculations
         to avoid integer overflows.

      ======================================================================

            flar Jim Graham
            aycsunw Ayc Ayc (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: