Changeset: 83f683229090 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=83f683229090
Modified Files:
        geom/monetdb5/geom.c
Branch: sfcgal
Log Message:

Fix few leaks. Verify if all GDKmalloc, GDKrealloc, and GDKzalloc succeeded 
(only filter joins were left for the second round) it help to detect few corner 
cases leading to incorrect results.


diffs (truncated from 558 to 300 lines):

diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c
--- a/geom/monetdb5/geom.c
+++ b/geom/monetdb5/geom.c
@@ -1802,15 +1802,22 @@ dumpPointsPoint(BAT *idBAT, BAT *geomBAT
 {
        char *newPath = NULL;
        size_t pathLength = strlen(path);
-       wkb *pointWKB = geos2wkb(geosGeometry);
+       wkb *pointWKB = NULL;
        int lvlDigitsNum = 10;  //MAX_UNIT = 4,294,967,295
        str err = MAL_SUCCEED;
 
        (*lvl)++;
 
-       newPath = GDKmalloc(pathLength + lvlDigitsNum + 1);
+       if ( (newPath = GDKmalloc(pathLength + lvlDigitsNum + 1)) == NULL) {
+        throw(MAL, "geom.Dump", MAL_MALLOC_FAIL); 
+    }
        sprintf(newPath, "%s%u", path, *lvl);
 
+       if ( (pointWKB = geos2wkb(geosGeometry)) == NULL) {
+           GDKfree(newPath);
+        throw(MAL, "geom.Dump", "geos2wkb failed");
+    }
+
        if (BUNappend(idBAT, newPath, TRUE) != GDK_SUCCEED ||
            BUNappend(geomBAT, pointWKB, TRUE) != GDK_SUCCEED)
                err = createException(MAL, "geom.Dump", "BUNappend failed");
@@ -1870,7 +1877,9 @@ dumpPointsPolygon(BAT *idBAT, BAT *geomB
 
        (*lvl)++;
 
-       newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 1);
+       if ( (newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 1)) 
== NULL) {
+        throw(MAL, "geom.DumpPoints", MAL_MALLOC_FAIL); 
+    }
        sprintf(newPath, "%s%u%s", path, *lvl, extraStr);
 
        //get the points in the exterior ring
@@ -1889,7 +1898,9 @@ dumpPointsPolygon(BAT *idBAT, BAT *geomB
                (*lvl)++;
                lvlDigitsNum = 10;      //MAX_UNIT = 4,294,967,295
 
-               newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 
1);
+               if ( (newPath = GDKmalloc(pathLength + lvlDigitsNum + 
extraLength + 1)) == NULL) {
+            throw(MAL, "geom.DumpPoints", MAL_MALLOC_FAIL); 
+        }
                sprintf(newPath, "%s%u%s", path, *lvl, extraStr);
 
                err = dumpPointsLineString(idBAT, geomBAT, 
GEOSGetInteriorRingN(geosGeometry, i), num_points, newPath);
@@ -1922,7 +1933,9 @@ dumpPointsMultiGeometry(BAT *idBAT, BAT 
                multiGeometry = GEOSGetGeometryN(geosGeometry, i);
                lvl++;
 
-               newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 
1);
+               if ( (newPath = GDKmalloc(pathLength + lvlDigitsNum + 
extraLength + 1)) == NULL) {
+            throw(MAL, "geom.DumpPoints", MAL_MALLOC_FAIL); 
+        }
                sprintf(newPath, "%s%u%s", path, lvl, extraStr);
 
                //*secondLevel = 0;
@@ -2286,10 +2299,18 @@ wkbPolygonize_(wkb** outWKB, wkb** geom1
             }
             break;
         default:
+            GEOSGeom_destroy(geos1Geometry);
+            GEOSGeom_destroy(geos2Geometry);
+            *outWKB = NULL;
             return createException(MAL, "geom.wkbPolygonize", "%s Unknown 
geometry type", geom_type2str(geos2Type,0));
     }
 
-       multiGeometry = GDKzalloc(sizeof(GEOSGeometry*) * (geos1Num + 
geos2Num));
+    if ( (multiGeometry = GDKzalloc(sizeof(GEOSGeometry*) * (geos1Num + 
geos2Num))) == NULL) {
+        GEOSGeom_destroy(geos1Geometry);
+        GEOSGeom_destroy(geos2Geometry);
+        *outWKB = NULL;
+        throw(MAL, "geom.wkbPolygonize_", MAL_MALLOC_FAIL);
+    }
 
     for(i=0; i < geos1Num; i++) {
         if (single1)
@@ -2365,13 +2386,16 @@ wkbsubPolygonize(bat *outBAT_id, bat* bB
     }
 
     /*Create the empty geoms*/
-    empty_geoms = (wkb**) GDKmalloc(sizeof(wkb*)*ngrp);
+    if ( ngrp && (empty_geoms = (wkb**) GDKmalloc(sizeof(wkb*)*ngrp)) == NULL) 
{
+            throw(MAL, "geom.wkbPolygonize", MAL_MALLOC_FAIL);
+    }
     for (i = 0; i < ngrp; i++)
         empty_geoms[i] = 
geos2wkb(GEOSGeom_createEmptyCollection(wkbGeometryCollection_mdb - 1));
 
     err = BATgroupWKBWKBtoWKB(outBAT_id, b, g, e, skip_nils, min, max, ngrp, 
start, end, empty_geoms, wkbPolygonize_, "wkbPolygonize");
        BBPkeepref(*outBAT_id);
 
+    /*TODO: Maybe free all geoms*/
     GDKfree(empty_geoms);
     BBPunfix(b->batCacheid);
     BBPunfix(g->batCacheid);
@@ -2661,7 +2685,8 @@ wkbFROMSTR_withSRID(char *geomWKT, int *
        if (strncasecmp(geomWKT, polyhedralSurface, strlen(polyhedralSurface)) 
== 0) {
                size_t sizeOfInfo = strlen(geomWKT) - strlen(polyhedralSurface);
                geomWKT_original = geomWKT;
-               geomWKT = GDKmalloc(sizeOfInfo + strlen(multiPolygon) + 1);
+               if ( (geomWKT = GDKmalloc(sizeOfInfo + strlen(multiPolygon) + 
1)) == NULL)
+            throw(MAL, "wkb.FromText", MAL_MALLOC_FAIL);
                strcpy(geomWKT, multiPolygon);
                memcpy(geomWKT + strlen(multiPolygon), 
&geomWKT_original[strlen(polyhedralSurface)], sizeOfInfo);
                geomWKT[sizeOfInfo + strlen(multiPolygon)] = '\0';
@@ -2704,8 +2729,8 @@ wkbFROMSTR_withSRID(char *geomWKT, int *
        return MAL_SUCCEED;
 }
 
-static int
-wkbaFROMSTR_withSRID(char *fromStr, int *len, wkba **toArray, int srid)
+static str
+wkbaFROMSTR_withSRID(int *res, char *fromStr, int *len, wkba **toArray, int 
srid)
 {
        int items, i;
        size_t skipBytes = 0;
@@ -2716,7 +2741,8 @@ wkbaFROMSTR_withSRID(char *fromStr, int 
        memcpy(&items, fromStr, sizeof(int));
        skipBytes += sizeof(int);
 
-       *toArray = GDKmalloc(wkba_size(items));
+       if ( (*toArray = GDKmalloc(wkba_size(items))) == NULL)
+            throw(MAL, "geom.wkbaFROMSTR_withSRID", MAL_MALLOC_FAIL);
 
        for (i = 0; i < items; i++) {
                size_t parsedBytes;
@@ -2729,7 +2755,8 @@ wkbaFROMSTR_withSRID(char *fromStr, int 
        }
 
        assert(skipBytes <= GDK_int_max);
-       return (int) skipBytes;
+       *res = (int) skipBytes;
+    return MAL_SUCCEED;
 }
 
 /* create the WKB out of the GEOSGeometry
@@ -2979,7 +3006,8 @@ str bbox3DFromGeos(bbox3D **out, const G
     bbox3D *bbox;
     str err;
 
-    bbox = (bbox3D*) GDKmalloc(sizeof(bbox3D));
+    if ( (bbox = (bbox3D*) GDKmalloc(sizeof(bbox3D))) == NULL)
+            throw(MAL, "geom.MinMaxZ", MAL_MALLOC_FAIL);
 
     geomMBR = mbrFromGeos(geosGeometry);
        if (mbr_isnil(geomMBR)){
@@ -3233,8 +3261,8 @@ wkbMLineStringToPolygon(wkb **geomWKB, s
        str ret = MAL_SUCCEED;
        wkb *inputWKB = NULL;
 
-       wkb **linestringsWKB;
-       double *linestringsArea;
+       wkb **linestringsWKB = NULL;
+       double *linestringsArea = NULL;
        bit ordered = 0;
 
        if (strcmp(*geomWKT, str_nil) == 0 || *srid == int_nil || *flag == 
int_nil) {
@@ -3257,8 +3285,10 @@ wkbMLineStringToPolygon(wkb **geomWKB, s
                return ret;
        }
 
-       linestringsWKB = GDKmalloc(itemsNum * sizeof(wkb *));
-       linestringsArea = GDKmalloc(itemsNum * sizeof(double));
+    if (itemsNum) {
+       linestringsWKB = GDKmalloc(itemsNum * sizeof(wkb *));
+       linestringsArea = GDKmalloc(itemsNum * sizeof(double));
+    }
        if (linestringsWKB == NULL || linestringsArea == NULL) {
                itemsNum = 0;
                ret = createException(MAL, "geom.MLineStringToPolygon", 
MAL_MALLOC_FAIL);
@@ -3324,7 +3354,7 @@ wkbMLineStringToPolygon(wkb **geomWKB, s
        if (*flag == 0) {
                //the biggest polygon is the external shell
                GEOSCoordSeq coordSeq_external;
-               GEOSGeom externalGeometry, linearRingExternalGeometry, 
*internalGeometries, finalGeometry;
+               GEOSGeom externalGeometry, linearRingExternalGeometry, 
*internalGeometries = NULL, finalGeometry;
 
                externalGeometry = wkb2geos(linestringsWKB[0]);
                if (externalGeometry == NULL) {
@@ -3346,7 +3376,8 @@ wkbMLineStringToPolygon(wkb **geomWKB, s
                }
 
                //all remaining should be internal
-               internalGeometries = GDKmalloc((itemsNum - 1) * sizeof(GEOSGeom 
*));
+        if ((itemsNum - 1) > 0)
+               internalGeometries = GDKmalloc((itemsNum - 1) * sizeof(GEOSGeom 
*));
                if (internalGeometries == NULL) {
                        GEOSGeom_destroy(linearRingExternalGeometry);
                        ret = createException(MAL, "geom.MLineStringToPolygon", 
MAL_MALLOC_FAIL);
@@ -5083,14 +5114,20 @@ wkbsubUnion(bat *outBAT_id, bat* bBAT_id
     }
 
     /*Create the empty geoms*/
-    empty_geoms = (wkb**) GDKmalloc(sizeof(wkb*)*ngrp);
+    if ( ngrp && (empty_geoms = (wkb**) GDKmalloc(sizeof(wkb*)*ngrp)) == NULL) 
{
+        BBPunfix(b->batCacheid);
+        BBPunfix(g->batCacheid);
+        BBPunfix(e->batCacheid);
+        throw(MAL, "geom.wkbsubUnion", MAL_MALLOC_FAIL);
+    }
     for (i = 0; i < ngrp; i++)
         empty_geoms[i] = geos2wkb(GEOSGeom_createEmptyPolygon());
 
     err = BATgroupWKBWKBtoWKB(outBAT_id, b, g, e, skip_nils, min, max, ngrp, 
start, end, empty_geoms, wkbUnion, "wkbUnion");
        BBPkeepref(*outBAT_id);
 
-    GDKfree(empty_geoms);
+    if (empty_geoms)
+        GDKfree(empty_geoms);
     BBPunfix(b->batCacheid);
     BBPunfix(g->batCacheid);
     BBPunfix(e->batCacheid);
@@ -5648,7 +5685,11 @@ wkbCollectAppend(wkb **out, wkb **geom1W
             GEOSGeom_destroy(geom2Geometry);
             throw(MAL, "geom.Collect", "GEOSGetNumGeometries failed");
         }
-        geomGeometries = (GEOSGeometry **) GDKmalloc(sizeof(GEOSGeometry 
*)*(num_geoms+1));
+        if ( (geomGeometries = (GEOSGeometry **) GDKmalloc(sizeof(GEOSGeometry 
*)*(num_geoms+1))) == NULL) {
+            GEOSGeom_destroy(geom1Geometry);
+            GEOSGeom_destroy(geom2Geometry);
+            throw(MAL, "geom.Collect", MAL_MALLOC_FAIL);
+        }
 
         for (i = 0; i < num_geoms; i++)
             geomGeometries[i] = GEOSGeom_clone(GEOSGetGeometryN(geom1Geometry, 
i));
@@ -5680,7 +5721,7 @@ wkbsubCollect(bat *outBAT_id, bat* bBAT_
     str err;
     BAT *b = NULL, *g = NULL, *e = NULL;
     oid min, max;
-    BUN ngrp;
+    BUN ngrp = 0;
     BUN start, end, cnt;
     wkb **empty_geoms = NULL;
     const oid *cand = NULL, *candend = NULL;
@@ -5703,9 +5744,14 @@ wkbsubCollect(bat *outBAT_id, bat* bBAT_
                     &cand, &candend)) != NULL) {
         throw(MAL, "BATgroupCollect: %s\n", msg);
     }
-
+    
     /*Create the empty geoms*/
-    empty_geoms = (wkb**) GDKmalloc(sizeof(wkb*)*ngrp);
+    if ( ngrp && (empty_geoms = (wkb**) GDKmalloc(sizeof(wkb*)*ngrp)) == NULL) 
{
+        BBPunfix(b->batCacheid);
+        BBPunfix(g->batCacheid);
+        BBPunfix(e->batCacheid);
+        throw(MAL, "geom.wkbsubCollect", MAL_MALLOC_FAIL);
+    }
     for (i = 0; i < ngrp; i++)
         empty_geoms[i] = 
geos2wkb(GEOSGeom_createEmptyCollection(wkbGeometryCollection_mdb - 1));
 
@@ -6406,7 +6452,11 @@ wkbBox2D(mbr **box, wkb **point1, wkb **
                err = createException(MAL, "geom.MakeBox2D", "Error in reading 
the points' coordinates");
        } else {
                //Assign the coordinates. Ensure that they are in correct order
-               *box = GDKmalloc(sizeof(mbr));
+               if ( (*box = GDKmalloc(sizeof(mbr))) == NULL) {
+               GEOSGeom_destroy(point1_geom);
+               GEOSGeom_destroy(point2_geom);
+            throw(MAL, "geom.wkbBox2D", MAL_MALLOC_FAIL);
+        }
                (*box)->xmin = (float) (xmin < xmax ? xmin : xmax);
                (*box)->ymin = (float) (ymin < ymax ? ymin : ymax);
                (*box)->xmax = (float) (xmax > xmin ? xmax : xmin);
@@ -6918,14 +6968,21 @@ wkbTOSTR(char **geomWKT, int *len, wkb *
                if (*len < (int) dstStrLen + 1) {
                        *len = (int) dstStrLen + 1;
                        GDKfree(*geomWKT);
-                       *geomWKT = GDKmalloc(*len);
+                       if ( (*geomWKT = GDKmalloc(*len)) == NULL) {
+                       GEOSFree(wkt);
+                //throw(MAL, "geom.wkbTOSTR", MAL_MALLOC_FAIL);
+                return 0;
+            }
                }
                snprintf(*geomWKT, *len, "\"%s\"", wkt);
                GEOSFree(wkt);
        } else {
                if (*len < 4) {
                        GDKfree(*geomWKT);
-                       *geomWKT = GDKmalloc(*len = 4);
+                       if ( (*geomWKT = GDKmalloc(*len = 4)) == NULL) {
+                //throw(MAL, "geom.wkbTOSTR", MAL_MALLOC_FAIL);
+                return 0;
+            }
                }
                strcpy(*geomWKT, "nil");
        }
@@ -7083,7 +7140,10 @@ mbrTOSTR(char **dst, int *len, mbr *atom
 
        if (*len < (int) dstStrLen + 1 || *dst == NULL) {
                GDKfree(*dst);
-               *dst = GDKmalloc(*len = (int) dstStrLen + 1);
+        if ( (*dst = GDKmalloc(*len = (int) dstStrLen + 1)) == NULL) {
+            //throw(MAL, "geom.mbrTOSTR", MAL_MALLOC_FAIL);
+            return 0;
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to