]> granicus.if.org Git - postgis/commitdiff
Throw on invalid characters when decoding geohash
authorRaúl Marín Rodríguez <rmrodriguez@carto.com>
Thu, 13 Jun 2019 11:57:13 +0000 (11:57 +0000)
committerRaúl Marín Rodríguez <rmrodriguez@carto.com>
Thu, 13 Jun 2019 11:57:13 +0000 (11:57 +0000)
Closes #4406
Closes https://github.com/postgis/postgis/pull/420

git-svn-id: http://svn.osgeo.org/postgis/trunk@17529 b70326c6-7e19-0410-871a-916f4a2858ee

NEWS
liblwgeom/cunit/cu_algorithm.c
liblwgeom/lwalgorithm.c

diff --git a/NEWS b/NEWS
index 2b759e218561658e563f8f59068acf69fd552594..38911a1d9347edbe33e997c4f47d904807f7bf8c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -22,6 +22,7 @@ Additional features enabled if you are running Proj6+ and PostgreSQL 12
   - #4334, Fix upgrade issues related to renamed parameters (Raúl Marín)
   - #4388, AddRasterConstraints: Ignore NULLs when generating constraints (Raúl Marín)
   - #4327, Avoid pfree'ing the result of getenv (Raúl Marín)
+  - #4406, Throw on invalid characters when decoding geohash (Raúl Marín)
 
 PostGIS 3.0.0alpha1
 2019/05/26
@@ -168,6 +169,7 @@ PostGIS 3.0.0
   - #4334, Fix upgrade issues related to renamed function parameters (Raúl Marín)
   - #4388, AddRasterConstraints: Ignore NULLs when generating constraints (Raúl Marín)
   - #4327, Avoid pfree'ing the result of getenv (Raúl Marín)
+  - #4406, Throw on invalid characters when decoding geohash (Raúl Marín)
 
 
 PostGIS 2.5.0
index c7f02dbce57d46bbb6ffa4869b244a893fd479a6..5f6f3374619ef46cd7fff7bef8a3730a3bad253c 100644 (file)
@@ -1138,6 +1138,23 @@ static void test_geohash_point_as_int(void)
        CU_ASSERT_EQUAL(gh, rs);
 }
 
+static void
+test_geohash_bbox(void)
+{
+       double lat[2], lon[2];
+
+       /* SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(-126,48),4326)) */
+       decode_geohash_bbox("c0w3hf1s70w3hf1s70w3", lat, lon, 100);
+       CU_ASSERT_DOUBLE_EQUAL(lat[0], 48, 1e-11);
+       CU_ASSERT_DOUBLE_EQUAL(lat[1], 48, 1e-11);
+       CU_ASSERT_DOUBLE_EQUAL(lon[0], -126, 1e-11);
+       CU_ASSERT_DOUBLE_EQUAL(lon[1], -126, 1e-11);
+
+       cu_error_msg_reset();
+       decode_geohash_bbox("@@@@@@", lat, lon, 100);
+       ASSERT_STRING_EQUAL(cu_error_msg, "decode_geohash_bbox: Invalid character '@'");
+}
+
 static void test_lwgeom_remove_repeated_points(void)
 {
        LWGEOM *g;
@@ -1698,6 +1715,7 @@ void algorithms_suite_setup(void)
        PG_ADD_TEST(suite,test_geohash_precision);
        PG_ADD_TEST(suite,test_geohash);
        PG_ADD_TEST(suite,test_geohash_point_as_int);
+       PG_ADD_TEST(suite, test_geohash_bbox);
        PG_ADD_TEST(suite,test_isclosed);
        PG_ADD_TEST(suite,test_lwgeom_simplify);
        PG_ADD_TEST(suite,test_lw_arc_center);
index 172014418e36b536082fa93381204f5eabeb83ff..f81f492762bcd705eb577b7a70f4d472cff9383b 100644 (file)
@@ -709,6 +709,7 @@ unsigned int geohash_point_as_int(POINT2D *pt)
 ** set in them will be the southwest and northeast coordinates of the bounding
 ** box accordingly. A precision less than 0 indicates that the entire length
 ** of the GeoHash should be used.
+** It will call `lwerror` if an invalid character is found
 */
 void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
 {
@@ -731,6 +732,13 @@ void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
        for (i = 0; i < precision; i++)
        {
                c = tolower(geohash[i]);
+               /* Valid characters are all digits and letters except a, i, l and o */
+               if (!(((c >= '0') && (c <= '9')) ||
+                     ((c >= 'b') && (c <= 'z') && (c != 'i') && (c != 'l') && (c != 'o'))))
+               {
+                       lwerror("%s: Invalid character '%c'", __func__, geohash[i]);
+                       return;
+               }
                cd = strchr(base32, c) - base32;
 
                for (j = 0; j < 5; j++)