Fix crash in close_ps() for NaN input coordinates.
authorTom Lane <[email protected]>
Sat, 16 Jul 2016 18:42:37 +0000 (14:42 -0400)
committerTom Lane <[email protected]>
Sat, 16 Jul 2016 18:42:37 +0000 (14:42 -0400)
The Assert() here seems unreasonably optimistic.  Andreas Seltenreich
found that it could fail with NaNs in the input geometries, and it
seems likely to me that it might fail in corner cases due to roundoff
error, even for ordinary input values.  As a band-aid, make the function
return SQL NULL instead of crashing.

Report: <[email protected]>

src/backend/utils/adt/geo_ops.c

index fdbeaadbb5597151e32e2151759e19390e6fb3b1..55fce191fb0903a4ef6bdfa5c0647d1d434e7942 100644 (file)
@@ -2913,7 +2913,7 @@ close_ps(PG_FUNCTION_ARGS)
        }
 
        /*
-        * at this point the "normal" from point will hit lseg. The closet point
+        * at this point the "normal" from point will hit lseg. The closest point
         * will be somewhere on the lseg
         */
        tmp = line_construct_pm(pt, invm);
@@ -2922,7 +2922,15 @@ close_ps(PG_FUNCTION_ARGS)
                   tmp->A, tmp->B, tmp->C, tmp->m);
 #endif
        result = interpt_sl(lseg, tmp);
-       Assert(result != NULL);
+
+       /*
+        * ordinarily we should always find an intersection point, but that could
+        * fail in the presence of NaN coordinates, and perhaps even from simple
+        * roundoff issues.  Return a SQL NULL if so.
+        */
+       if (result == NULL)
+               PG_RETURN_NULL();
+
 #ifdef GEODEBUG
        printf("close_ps- result.x %f  result.y %f\n", result->x, result->y);
 #endif