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

Raul raul at rmr.ninja
Thu Jun 13 04:57:14 PDT 2019


Author: algunenano
Date: 2019-06-13 04:57:13 -0700 (Thu, 13 Jun 2019)
New Revision: 17529

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

Closes #4406
Closes https://github.com/postgis/postgis/pull/420


Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2019-06-13 11:56:02 UTC (rev 17528)
+++ trunk/NEWS	2019-06-13 11:57:13 UTC (rev 17529)
@@ -22,6 +22,7 @@
   - #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 @@
   - #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

Modified: trunk/liblwgeom/cunit/cu_algorithm.c
===================================================================
--- trunk/liblwgeom/cunit/cu_algorithm.c	2019-06-13 11:56:02 UTC (rev 17528)
+++ trunk/liblwgeom/cunit/cu_algorithm.c	2019-06-13 11:57:13 UTC (rev 17529)
@@ -1138,6 +1138,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_remove_repeated_points(void)
 {
 	LWGEOM *g;
@@ -1698,6 +1715,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: trunk/liblwgeom/lwalgorithm.c
===================================================================
--- trunk/liblwgeom/lwalgorithm.c	2019-06-13 11:56:02 UTC (rev 17528)
+++ trunk/liblwgeom/lwalgorithm.c	2019-06-13 11:57:13 UTC (rev 17529)
@@ -709,6 +709,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)
 {
@@ -731,6 +732,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