[postgis-tickets] r16549 - ST_ClusterKMeans crash on NULL and EMPTY

Darafei komzpa at gmail.com
Sat Apr 21 06:17:14 PDT 2018


Author: komzpa
Date: 2018-04-21 06:17:14 -0700 (Sat, 21 Apr 2018)
New Revision: 16549

Modified:
   branches/2.3/NEWS
   branches/2.3/liblwgeom/lwkmeans.c
   branches/2.3/regress/lwgeom_regress.sql
   branches/2.3/regress/lwgeom_regress_expected
Log:
ST_ClusterKMeans crash on NULL and EMPTY

Closes #4071


Modified: branches/2.3/NEWS
===================================================================
--- branches/2.3/NEWS	2018-04-21 12:36:51 UTC (rev 16548)
+++ branches/2.3/NEWS	2018-04-21 13:17:14 UTC (rev 16549)
@@ -1,3 +1,11 @@
+PostGIS 2.3.8
+2018/xx/xx
+
+  * Bug Fixes and Enchantments
+
+  - #4071, ST_ClusterKMeans crash on NULL/EMPTY fixed (Darafei Praliaskouski)
+
+
 PostGIS 2.3.7
 2018/04/06
 

Modified: branches/2.3/liblwgeom/lwkmeans.c
===================================================================
--- branches/2.3/liblwgeom/lwkmeans.c	2018-04-21 12:36:51 UTC (rev 16548)
+++ branches/2.3/liblwgeom/lwkmeans.c	2018-04-21 13:17:14 UTC (rev 16549)
@@ -92,7 +92,7 @@
 	assert(k>0);
 	assert(ngeoms>0);
 	assert(geoms);
-	
+
 	if (ngeoms<k)
 	{
 		lwerror("%s: number of geometries is less than the number of clusters requested", __func__);
@@ -182,7 +182,7 @@
 
 		/* Find the data point closest to the calculated point */
 		closest = lwkmeans_pt_closest(config.objs, config.num_objs, &p);
-		
+
 		/* If something is terrible wrong w/ data, cannot find a closest */
 		if (closest < 0)
 			lwerror("unable to calculate cluster seed points, too many NULLs or empties?");
@@ -193,7 +193,16 @@
 		{
 			if (seen[j] == closest)
 			{
-				closest = (closest + 1) % config.num_objs;
+				int k, t;
+				for (k = 1; k < config.num_objs; k++)
+				{
+					t = (closest + k) % config.num_objs;
+					if (config.objs[t])
+					{
+						closest = t;
+						break;
+					}
+				}
 				j = 0;
 			}
 			else

Modified: branches/2.3/regress/lwgeom_regress.sql
===================================================================
--- branches/2.3/regress/lwgeom_regress.sql	2018-04-21 12:36:51 UTC (rev 16548)
+++ branches/2.3/regress/lwgeom_regress.sql	2018-04-21 13:17:14 UTC (rev 16549)
@@ -135,3 +135,11 @@
 SELECT 'BoundingDiagonal6', ST_AsEwkt(ST_BoundingDiagonal(
     'SRID=3857;POLYGON M EMPTY'::geometry
 ));
+
+-- check that null and empty is handled in the clustering
+select '#4071', count(distinct a), count(distinct b), count(distinct c)  from
+(select
+	ST_ClusterKMeans(geom, 1) over () a,
+	ST_ClusterKMeans(geom, 2) over () b,
+	ST_ClusterKMeans(geom, 3) over () c
+from (values (null::geometry), ('POINT(1 1)'), ('POINT EMPTY'), ('POINT(0 0)'), ('POINT(4 4)')) as g (geom)) z;

Modified: branches/2.3/regress/lwgeom_regress_expected
===================================================================
--- branches/2.3/regress/lwgeom_regress_expected	2018-04-21 12:36:51 UTC (rev 16548)
+++ branches/2.3/regress/lwgeom_regress_expected	2018-04-21 13:17:14 UTC (rev 16549)
@@ -21,3 +21,4 @@
 BoundingDiagonal4|SRID=3857;LINESTRING(-1 -2 -8 2,1 2 3 9)
 BoundingDiagonal5|SRID=3857;LINESTRINGM(4 4 0,5 4 1)
 BoundingDiagonal6|SRID=3857;LINESTRINGM EMPTY
+#4071|2|3|4



More information about the postgis-tickets mailing list