[postgis-tickets] r17526 - Throw on invalid characters when decoding geohash

Raul raul at rmr.ninja
Thu Jun 13 04:54:36 PDT 2019


Author: algunenano
Date: 2019-06-13 04:54:34 -0700 (Thu, 13 Jun 2019)
New Revision: 17526

Modified:
   branches/2.3/NEWS
   branches/2.3/liblwgeom/cunit/cu_algorithm.c
   branches/2.3/liblwgeom/lwalgorithm.c
Log:
Throw on invalid characters when decoding geohash

References #4406


Modified: branches/2.3/NEWS
===================================================================
--- branches/2.3/NEWS	2019-06-13 07:44:49 UTC (rev 17525)
+++ branches/2.3/NEWS	2019-06-13 11:54:34 UTC (rev 17526)
@@ -7,6 +7,7 @@
   - #4326, Fix circular arc distance calculation (Paul Ramsey)
   - #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.3.9

Modified: branches/2.3/liblwgeom/cunit/cu_algorithm.c
===================================================================
--- branches/2.3/liblwgeom/cunit/cu_algorithm.c	2019-06-13 07:44:49 UTC (rev 17525)
+++ branches/2.3/liblwgeom/cunit/cu_algorithm.c	2019-06-13 11:54:34 UTC (rev 17526)
@@ -925,6 +925,23 @@
 	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_simplify(void)
 {
 		LWGEOM *l;
@@ -1205,6 +1222,7 @@
 	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);

Modified: branches/2.3/liblwgeom/lwalgorithm.c
===================================================================
--- branches/2.3/liblwgeom/lwalgorithm.c	2019-06-13 07:44:49 UTC (rev 17525)
+++ branches/2.3/liblwgeom/lwalgorithm.c	2019-06-13 11:54:34 UTC (rev 17526)
@@ -699,6 +699,7 @@
 ** 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)
 {
@@ -721,6 +722,13 @@
 	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++)



More information about the postgis-tickets mailing list