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

java.awt.Rectangle should explain the possible field overflow during calculation

    Details

    • Subcomponent:
      2d
    • Resolved In Build:
      b86
    • CPU:
      x86, sparc
    • OS:
      linux_redhat_7.2, solaris_2.6

      Description

      Name: dsR10051 Date: 08/13/2001



      Javadoc for class java.awt.Rectangle
      should document that methods

      public void add(int newx, int newy)
      public void add(Point pt)
      public void add(Rectangle r)
      public Rectangle union(Rectangle r)
      public void translate(int x, int y)
      public void grow(int h, int v)

      ignores the internal fields overflow and underflow during the calculations.

      For example, the implementation of method translate(int x, int y) is:
          public void translate(int x, int y) {
              this.x += x;
              this.y += y;
          }
      If this.x + x > Integer.MAX_VALUE, the additive operator is overflow and
      new x coordinate is not equal to expected, mathematical correct result.

      ======================================================================
      Look at following tests:

      === 1 ===
      public class Test {
          public static void main(String[] args) throws Exception {
              Rectangle rec = new Rectangle(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
              rec.add(-1, -1);
              System.out.println("rec: " + rec);
          }
      }

      rec: java.awt.Rectangle[x=-1,y=-1,width=-2147483648,height=-2147483648]

      As a result we have a rectangle with negative width and height. It is unusual.

      === 2 ===

      public class Test {
          public static void main(String[] args) throws Exception {
              Rectangle rec = new Rectangle(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
              rec.union(new Rectangle(-1, -1, 1, 1));
              System.out.println("rec: " + rec);
          }
      }

      rec: java.awt.Rectangle[x=0,y=0,width=2147483647,height=2147483647]

      As a result a rectangle doesn't have a point [-1, -1]. It is unusual.

      === 3 ===

      public class Test {
          public static void main(String[] args) throws Exception {
              Rectangle rec = new Rectangle(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
              rec.grow(1, 1);
              System.out.println("rec: " + rec);
          }
      }

      rec: java.awt.Rectangle[x=-1,y=-1,width=-2147483647,height=-2147483647]

      As a result we have a rectangle with negative width and height. It is unusual.

      === 4 ===

      public class Test {
          public static void main(String[] args) throws Exception {
              Rectangle rec = new Rectangle(Integer.MAX_VALUE, Integer.MAX_VALUE, 1, 1);
              rec.translate(1, 1);
              System.out.println("rec: " + rec);
          }
      }

      rec: java.awt.Rectangle[x=-2147483648,y=-2147483648,width=1,height=1]

      As a result we have a rectangle starting with [-2147483648,-2147483648] whereas previous start point was positive. It is unusual.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                flar Jim Graham
                Reporter:
                sdasunw Sda Sda (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: