Describe the Bug
wktToPolygon explicitly returns null for null/empty input and for "no parseable points", establishing a "null = invalid" contract. But for other malformed WKT it throws instead of returning null:
- missing
((/)) → substring(indexOf("((")+2, lastIndexOf("))")) becomes substring(start, -1) → StringIndexOutOfBoundsException
- non-numeric coordinates →
Double.parseDouble(...) → NumberFormatException
In practice the WKT is read from the PostGIS polygon column (always well-formed), so the trigger is low (DB corruption / manual edits), but it is an inconsistent contract.
Root Cause
public List<GeoPoint> wktToPolygon(String wkt) {
if (wkt == null || wkt.trim().isEmpty()) return null;
String coordinates = wkt.substring(wkt.indexOf("((") + 2, wkt.lastIndexOf("))")); // -1 → StringIndexOOB
String[] points = coordinates.split(",");
List<GeoPoint> polygon = new ArrayList<>();
for (String point : points) {
String[] coords = point.trim().split("\\s+");
if (coords.length >= 2) {
double longitude = Double.parseDouble(coords[0]); // non-numeric → NumberFormatException
double latitude = Double.parseDouble(coords[1]);
polygon.add(GeoPoint.from(latitude, longitude));
}
}
return polygon.isEmpty() ? null : polygon;
}
Expected vs Observed
Expected: malformed WKT → null (consistent with the existing null/empty handling). Observed: StringIndexOutOfBoundsException / NumberFormatException.
Suggested Fix
Validate ((/)) presence and wrap parsing in try/catch returning null (or Optional.empty()), matching the sibling read() method which already catches NumberFormatException.
Corresponding Test (generated)
@Test
public void testWktToPolygon_ValidFormatButInvalidDoubleParsing_ReturnsNull() {
String wkt = "POLYGON((abc def, ghi jkl))";
List<GeoPoint> result = pointReaderWriter.wktToPolygon(wkt);
assertNull(result);
}
@Test
public void testWktToPolygon_InvalidFormat_NoClosingParentheses_ReturnsNull() {
String wkt = "POLYGON((lon1 lat1, lon2 lat2";
List<GeoPoint> result = pointReaderWriter.wktToPolygon(wkt);
assertNull(result);
}
@Test
public void testWktToPolygon_InvalidFormat_NoOpeningParentheses_ReturnsNull() {
String wkt = "POLYGON lon1 lat1, lon2 lat2))";
List<GeoPoint> result = pointReaderWriter.wktToPolygon(wkt);
assertNull(result);
}
@Test
public void testWktToPolygon_InvalidFormat_NoDoubleParentheses_ReturnsNull() {
String wkt = "POLYGON(lon1 lat1, lon2 lat2)";
List<GeoPoint> result = pointReaderWriter.wktToPolygon(wkt);
assertNull(result);
}
This input was generated by the test case generator TestFusion developed in our STAR lab.
Describe the Bug
wktToPolygonexplicitly returnsnullfor null/empty input and for "no parseable points", establishing a "null = invalid" contract. But for other malformed WKT it throws instead of returning null:((/))→substring(indexOf("((")+2, lastIndexOf("))"))becomessubstring(start, -1)→StringIndexOutOfBoundsExceptionDouble.parseDouble(...)→NumberFormatExceptionIn practice the WKT is read from the PostGIS
polygoncolumn (always well-formed), so the trigger is low (DB corruption / manual edits), but it is an inconsistent contract.Root Cause
Expected vs Observed
Expected: malformed WKT →
null(consistent with the existing null/empty handling). Observed:StringIndexOutOfBoundsException/NumberFormatException.Suggested Fix
Validate
((/))presence and wrap parsing in try/catch returning null (orOptional.empty()), matching the siblingread()method which already catchesNumberFormatException.Corresponding Test (generated)
This input was generated by the test case generator
TestFusiondeveloped in our STAR lab.