[PostGIS] #5943: Memory leak in handling GEOS GeometryFactory

PostGIS trac at osgeo.org
Tue Jul 15 07:52:30 PDT 2025


#5943: Memory leak in handling GEOS GeometryFactory
-------------------------+---------------------------
 Reporter:  meganxma     |      Owner:  pramsey
     Type:  defect       |     Status:  new
 Priority:  low          |  Milestone:  PostGIS 3.5.4
Component:  postgis      |    Version:  3.5.x
 Keywords:  memory leak  |
-------------------------+---------------------------
 Running installcheck tests with ASAN showed that there are multiple memory
 leaks in Postgis's handling of GEOS GeometryFactory.

 I've created a patch file for the fixes:

 {{{#!div style="font-size: 100%"
   {{{
          --- a/liblwgeom/lwgeom_geos.c  2025-01-07 10:09:06.000000000
 +0000
          +++ b/liblwgeom/lwgeom_geos.c  2025-01-07 10:07:11.000000000
 +0000
          @@ -723,7 +723,7 @@
                         g3 = GEOSIntersection(g1, g2);
                 }

          -      if (!g3) GEOS_FREE_AND_FAIL(g1);
          +      if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
                 GEOSSetSRID(g3, srid);

                 if (!(result = GEOS2LWGEOM(g3, is3d)))
 GEOS_FREE_AND_FAIL(g1, g2, g3);
          @@ -1145,7 +1145,7 @@
                 /* If no geometries are in result collection, return NULL
 */
                 if (GEOSGetNumGeometries(g3) == 0)
                 {
          -              GEOS_FREE(g1);
          +              GEOS_FREE(g1, g3);
                         return NULL;
                 }



          --- a/postgis/lwgeom_geos_predicates.c    2025-07-10
 19:51:54.478513150 +0000
          +++ b/postgis/lwgeom_geos_predicates.c    2025-07-10
 19:51:41.568325255 +0000
          @@ -978,7 +978,11 @@
                 POSTGIS_DEBUGF(3, "%s", GEOSGeomToWKT(g2));

                 relate_str = GEOSRelateBoundaryNodeRule(g1, g2, bnr);
          -      if (!relate_str) HANDLE_GEOS_ERROR("GEOSRelate");
          +      if (!relate_str) {
          +              GEOSGeom_destroy(g1);
          +              GEOSGeom_destroy(g2);
          +              HANDLE_GEOS_ERROR("GEOSRelate");
          +      }
                 result = cstring_to_text(relate_str);
                 GEOSFree(relate_str);
                 GEOSGeom_destroy(g1);
   }}}
 }}}
-- 
Ticket URL: <https://trac.osgeo.org/postgis/ticket/5943>
PostGIS <http://trac.osgeo.org/postgis/>
The PostGIS Trac is used for bug, enhancement & task tracking, a user and developer wiki, and a view into the subversion code repository of PostGIS project.


More information about the postgis-tickets mailing list