From c90ed8cb95f123d7cd52e821cb365aa0209a6b4c Mon Sep 17 00:00:00 2001 From: Andreas Muttscheller Date: Tue, 21 Oct 2025 16:22:47 +0200 Subject: [PATCH] Allow minus sign in point wkt Very small coordinates that contain three or more zeros after the dot (like 0.000123) have a string representation of 1.23E-4. The readPointWkt did not account for this and set the end pos to the E, thus the BigDecimal tried to parse "1.23E". This resulted in a NumberFormatException. This fixes the issue by also allowing the minus sign when determining the end of the number. --- .../sqlserver/jdbc/SQLServerSpatialDatatype.java | 3 ++- .../jdbc/datatypes/SQLServerSpatialDatatypeTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSpatialDatatype.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSpatialDatatype.java index 31f6eb95b6..37fd3e6bd1 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSpatialDatatype.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSpatialDatatype.java @@ -1484,7 +1484,8 @@ void readPointWkt() throws SQLServerException { while (currentWktPos < wkt.length() && (Character.isDigit(wkt.charAt(currentWktPos)) || wkt.charAt(currentWktPos) == '.' - || wkt.charAt(currentWktPos) == 'E' || wkt.charAt(currentWktPos) == 'e')) { + || wkt.charAt(currentWktPos) == 'E' || wkt.charAt(currentWktPos) == 'e' + || wkt.charAt(currentWktPos) == '-')) { currentWktPos++; } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java index 5448cda25f..87aa62843d 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java @@ -2122,6 +2122,18 @@ public void testLargeCases() throws SQLException { } } + /** + * Tests Geography almost zero coordinates like 0.0001234. The string representation is "1.234E-4", which + * caused a bug when creating a Geography object. + */ + @Test + public void testGeographySmallCoordinates() throws SQLException { + Geography g = Geography.point(0.0001234, 1.234, 4326); + + assertEquals(0.0001234, g.getLatitude()); + assertEquals(1.234, g.getLongitude()); + } + private void beforeEachSetup() throws SQLException { try (Statement stmt = connection.createStatement()) { TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(geomTableName), stmt);