Skip to content

Commit 9119357

Browse files
HeikoKlareakoch-yatta
authored andcommitted
Ensure that rounding mode of Point.OfFloat is never null
When initializing a Point.OfFloat with integer values, the rounding mode is currently set to null. This can lead to NPEs when cloning such points, as clone() uses the rounding mode to initialize a new instance, which expects the rounding mode to not be null. Setting values on the point with setX() and setY() will fail likewise. This change ensures that the rounding mode will never be initialized with null.
1 parent ce03c39 commit 9119357

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static sealed class OfFloat extends Point permits Point.WithMonitor {
143143

144144
public OfFloat(int x, int y) {
145145
super(x, y);
146-
this.roundingMode = null;
146+
this.roundingMode = RoundingMode.ROUND;
147147
}
148148

149149
public OfFloat(float x, float y) {

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Point.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,24 @@ public void test_toString() {
7373
assertTrue(p.toString().length() > 0);
7474
assertEquals("Point {3, 4}", p.toString());
7575
}
76+
77+
@Test
78+
public void test_OfFloat_clone() {
79+
Point.OfFloat pointOfInt = new Point.OfFloat(3, 4);
80+
Point.OfFloat clonedPointOfInt = pointOfInt.clone();
81+
assertEquals(pointOfInt, clonedPointOfInt);
82+
assertEquals(pointOfInt.x, clonedPointOfInt.x);
83+
assertEquals(pointOfInt.getX(), clonedPointOfInt.getX());
84+
assertEquals(pointOfInt.y, clonedPointOfInt.y);
85+
assertEquals(pointOfInt.getY(), clonedPointOfInt.getY());
86+
87+
Point.OfFloat pointOfFloat = new Point.OfFloat(3.4f, 3.5f);
88+
Point.OfFloat clonedPointOfFloat = pointOfFloat.clone();
89+
assertEquals(pointOfFloat, clonedPointOfFloat);
90+
assertEquals(pointOfFloat.x, clonedPointOfFloat.x);
91+
assertEquals(pointOfFloat.getX(), clonedPointOfFloat.getX());
92+
assertEquals(pointOfFloat.y, clonedPointOfFloat.y);
93+
assertEquals(pointOfFloat.getY(), clonedPointOfFloat.getY());
94+
}
95+
7696
}

0 commit comments

Comments
 (0)