Changeset: b7d9bb9f6799 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=b7d9bb9f6799
Modified Files:
        gdk/gdk_bat.c
        gdk/gdk_heap.c
        gdk/gdk_logger.c
        geom/monetdb5/geom.c
        geom/monetdb5/geom_upgrade.c
        monetdb5/mal/mal_builder.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_exception.c
        monetdb5/mal/mal_import.c
        monetdb5/mal/mal_instruction.c
        monetdb5/mal/mal_linker.c
        monetdb5/mal/mal_listing.c
        monetdb5/mal/mal_resolve.c
        monetdb5/mal/mal_runtime.c
        monetdb5/mal/mal_session.c
        monetdb5/modules/atoms/batxml.c
        monetdb5/modules/atoms/blob.c
        monetdb5/modules/atoms/color.c
        monetdb5/modules/atoms/identifier.c
        monetdb5/modules/atoms/inet.c
        monetdb5/modules/atoms/mtime.c
        monetdb5/modules/atoms/str.c
        monetdb5/modules/atoms/streams.c
        monetdb5/modules/atoms/url.c
        monetdb5/modules/atoms/uuid.c
        monetdb5/modules/atoms/xml.c
        monetdb5/modules/mal/mal_io.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/pcre.c
        monetdb5/modules/mal/remote.c
        monetdb5/modules/mal/tablet.c
        monetdb5/modules/mal/txtsim.c
        monetdb5/optimizer/opt_emptybind.c
        monetdb5/optimizer/opt_evaluate.c
        monetdb5/optimizer/opt_macro.c
        monetdb5/optimizer/opt_mergetable.c
        monetdb5/optimizer/opt_multiplex.c
        monetdb5/optimizer/opt_support.c
        monetdb5/scheduler/run_adder.c
        sql/backends/monet5/UDF/pyapi/convert_loops.h
        sql/backends/monet5/sql_execute.c
        sql/backends/monet5/sql_gencode.c
        sql/backends/monet5/sql_optimizer.c
        sql/backends/monet5/sql_scenario.c
        sql/backends/monet5/sql_upgrades.c
        sql/backends/monet5/sql_user.c
        sql/backends/monet5/vaults/bam/bam_export.c
        sql/backends/monet5/vaults/bam/bam_globals.h
        sql/backends/monet5/vaults/bam/bam_loader.c
        sql/backends/monet5/vaults/bam/bam_wrapper.c
        sql/common/sql_mem.c
        tools/merovingian/daemon/controlrunner.c
        tools/mserver/mserver5.c
Branch: Dec2016
Log Message:

Lots of error checking of GDKmalloc and GDKrealloc.
Plus all sorts of related fixes.


diffs (truncated from 2774 to 300 lines):

diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -284,7 +284,14 @@ BATattach(int tt, const char *heapfile, 
                while ((c = getc(f)) != EOF) {
                        if (n == m) {
                                m += 4096;
-                               p = GDKrealloc(p, m);
+                               s = GDKrealloc(p, m);
+                               if (s == NULL) {
+                                       GDKfree(p);
+                                       BBPreclaim(bn);
+                                       fclose(f);
+                                       return NULL;
+                               }
+                               p = s;
                                s = p + n;
                        }
                        if (c == '\n' && n > 0 && s[-1] == '\r') {
diff --git a/gdk/gdk_heap.c b/gdk/gdk_heap.c
--- a/gdk/gdk_heap.c
+++ b/gdk/gdk_heap.c
@@ -216,12 +216,12 @@ HEAPextend(Heap *h, size_t size, int may
                /* try GDKrealloc if the heap size stays within
                 * reasonable limits */
                if (!must_mmap) {
-                       void *p = h->base;
                        h->newstorage = h->storage = STORE_MEM;
                        h->base = GDKreallocmax(h->base, size, &h->size, 0);
-                       HEAPDEBUG fprintf(stderr, "#HEAPextend: extending 
malloced heap " SZFMT " " SZFMT " " PTRFMT " " PTRFMT "\n", size, h->size, 
PTRFMTCAST p, PTRFMTCAST h->base);
+                       HEAPDEBUG fprintf(stderr, "#HEAPextend: extending 
malloced heap " SZFMT " " SZFMT " " PTRFMT " " PTRFMT "\n", size, h->size, 
PTRFMTCAST bak.base, PTRFMTCAST h->base);
                        if (h->base)
                                return GDK_SUCCEED; /* success */
+                       /* bak.base is still valid and may get restored */
                        failure = "h->storage == STORE_MEM && !must_map && 
!h->base";
                }
                /* too big: convert it to a disk-based temporary heap */
@@ -630,7 +630,7 @@ HEAPload_intern(Heap *h, const char *nme
 {
        size_t minsize;
        int ret = 0;
-       char *srcpath, *dstpath;
+       char *srcpath, *dstpath, *tmp;
        int t0;
 
        h->storage = h->newstorage = h->size < 4 * GDK_mmap_pagesize ? 
STORE_MEM : STORE_MMAP;
@@ -677,7 +677,14 @@ HEAPload_intern(Heap *h, const char *nme
         * takes precedence. */
        srcpath = GDKfilepath(h->farmid, BATDIR, nme, ext);
        dstpath = GDKfilepath(h->farmid, BATDIR, nme, ext);
-       srcpath = GDKrealloc(srcpath, strlen(srcpath) + strlen(suffix) + 1);
+       if (srcpath == NULL ||
+           dstpath == NULL ||
+           (tmp = GDKrealloc(srcpath, strlen(srcpath) + strlen(suffix) + 1)) 
== NULL) {
+               GDKfree(srcpath);
+               GDKfree(dstpath);
+               return GDK_FAIL;
+       }
+       srcpath = tmp;
        strcat(srcpath, suffix);
 
        t0 = GDKms();
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -740,10 +740,14 @@ static int
 tr_grow(trans *tr)
 {
        if (tr->nr == tr->sz) {
+               logaction *tmp;
                tr->sz <<= 1;
+               tmp = tr->changes;
                tr->changes = (logaction *) GDKrealloc(tr->changes, tr->sz * 
sizeof(logaction));
-               if (tr->changes == NULL)
+               if (tr->changes == NULL) {
+                       GDKfree(tmp);
                        return 0;
+               }
        }
        /* cleanup the next */
        tr->changes[tr->nr].name = NULL;
diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c
--- a/geom/monetdb5/geom.c
+++ b/geom/monetdb5/geom.c
@@ -853,18 +853,18 @@ segmentizeLineString(GEOSGeometry **outG
        //store the points so that I do not have to read them multiple times 
using geos
        if ((xCoords_org = GDKmalloc(pointsNum * sizeof(double))) == NULL) {
                *outGeometry = NULL;
-               throw(MAL, "geom.Segmentize", "Could not allocate memory for %d 
double values", pointsNum);
+               throw(MAL, "geom.Segmentize", MAL_MALLOC_FAIL " for %d double 
values", pointsNum);
        }
        if ((yCoords_org = GDKmalloc(pointsNum * sizeof(double))) == NULL) {
                GDKfree(xCoords_org);
                *outGeometry = NULL;
-               throw(MAL, "geom.Segmentize", "Could not allocate memory for %d 
double values", pointsNum);
+               throw(MAL, "geom.Segmentize", MAL_MALLOC_FAIL " for %d double 
values", pointsNum);
        }
        if ((zCoords_org = GDKmalloc(pointsNum * sizeof(double))) == NULL) {
                GDKfree(xCoords_org);
                GDKfree(yCoords_org);
                *outGeometry = NULL;
-               throw(MAL, "geom.Segmentize", "Could not allocate memory for %d 
double values", pointsNum);
+               throw(MAL, "geom.Segmentize", MAL_MALLOC_FAIL " for %d double 
values", pointsNum);
        }
 
        if (!GEOSCoordSeq_getX(gcs_old, 0, &xCoords_org[0])) {
@@ -1744,9 +1744,16 @@ dumpPointsPoint(BAT *idBAT, BAT *geomBAT
        int lvlDigitsNum = 10;  //MAX_UNIT = 4,294,967,295
        str err = MAL_SUCCEED;
 
+       if (pointWKB == NULL)
+               throw(MAL, "geom.Dump", MAL_MALLOC_FAIL);
+
        (*lvl)++;
 
        newPath = GDKmalloc(pathLength + lvlDigitsNum + 1);
+       if (newPath == NULL) {
+               GDKfree(pointWKB);
+               throw(MAL, "geom.Dump", MAL_MALLOC_FAIL);
+       }
        sprintf(newPath, "%s%u", path, *lvl);
 
        if (BUNappend(idBAT, newPath, TRUE) != GDK_SUCCEED ||
@@ -1774,7 +1781,7 @@ dumpPointsLineString(BAT *idBAT, BAT *ge
        if (err != MAL_SUCCEED)
                return err;
 
-       for (i = 0; i < pointsNum; i++) {
+       for (i = 0; i < pointsNum && err == MAL_SUCCEED; i++) {
                GEOSGeometry *pointGeometry = GEOSGeomGetPointN(geosGeometry, 
i);
 
                if (pointGeometry == NULL)
@@ -1801,12 +1808,14 @@ dumpPointsPolygon(BAT *idBAT, BAT *geomB
 
        //get the exterior ring of the polygon
        exteriorRingGeometry = GEOSGetExteriorRing(geosGeometry);
-       if (!exteriorRingGeometry)
+       if (exteriorRingGeometry == NULL)
                throw(MAL, "geom.DumpPoints", "GEOSGetExteriorRing failed");
 
        (*lvl)++;
 
        newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 1);
+       if (newPath == NULL)
+               throw(MAL, "geom.DumpPoints", MAL_MALLOC_FAIL);
        sprintf(newPath, "%s%u%s", path, *lvl, extraStr);
 
        //get the points in the exterior ring
@@ -1818,7 +1827,7 @@ dumpPointsPolygon(BAT *idBAT, BAT *geomB
        //check the interior rings
        numInteriorRings = GEOSGetNumInteriorRings(geosGeometry);
        if (numInteriorRings == -1)
-               throw(MAL, "geom.NumPoints", "GEOSGetNumInteriorRings failed");
+               throw(MAL, "geom.DumpPoints", "GEOSGetNumInteriorRings failed");
 
        // iterate over the interiorRing and transform each one of them
        for (i = 0; i < numInteriorRings; i++) {
@@ -1826,6 +1835,8 @@ dumpPointsPolygon(BAT *idBAT, BAT *geomB
                lvlDigitsNum = 10;      //MAX_UNIT = 4,294,967,295
 
                newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 
1);
+               if (newPath == NULL)
+                       throw(MAL, "geom.DumpPoints", MAL_MALLOC_FAIL);
                sprintf(newPath, "%s%u%s", path, *lvl, extraStr);
 
                err = dumpPointsLineString(idBAT, geomBAT, 
GEOSGetInteriorRingN(geosGeometry, i), newPath);
@@ -1859,6 +1870,8 @@ dumpPointsMultiGeometry(BAT *idBAT, BAT 
                lvl++;
 
                newPath = GDKmalloc(pathLength + lvlDigitsNum + extraLength + 
1);
+               if (newPath == NULL)
+                       throw(MAL, "geom.DumpPoints", MAL_MALLOC_FAIL);
                sprintf(newPath, "%s%u%s", path, lvl, extraStr);
 
                //*secondLevel = 0;
@@ -2166,6 +2179,8 @@ wkbFROMSTR_withSRID(char *geomWKT, int *
                size_t sizeOfInfo = strlen(geomWKT) - strlen(polyhedralSurface);
                geomWKT_original = geomWKT;
                geomWKT = GDKmalloc(sizeOfInfo + strlen(multiPolygon) + 1);
+               if (geomWKT == 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';
@@ -2173,13 +2188,23 @@ wkbFROMSTR_withSRID(char *geomWKT, int *
        ////////////////////////// UP TO HERE ///////////////////////////
 
        WKT_reader = GEOSWKTReader_create();
+       if (WKT_reader == NULL) {
+               if (geomWKT_original)
+                       GDKfree(geomWKT);
+               throw(MAL, "wkb.FromText", "GEOSWKTReader_create failed");
+       }
        geosGeometry = GEOSWKTReader_read(WKT_reader, geomWKT);
        GEOSWKTReader_destroy(WKT_reader);
 
-       if (geosGeometry == NULL)
+       if (geosGeometry == NULL) {
+               if (geomWKT_original)
+                       GDKfree(geomWKT);
                throw(MAL, "wkb.FromText", "GEOSWKTReader_read failed");
+       }
 
        if (GEOSGeomTypeId(geosGeometry) == -1) {
+               if (geomWKT_original)
+                       GDKfree(geomWKT);
                GEOSGeom_destroy(geosGeometry);
                throw(MAL, "wkb.FromText", "GEOSGeomTypeId failed");
        }
@@ -2192,8 +2217,11 @@ wkbFROMSTR_withSRID(char *geomWKT, int *
         * want to get the wkb out of it */
        *geomWKB = geos2wkb(geosGeometry);
        GEOSGeom_destroy(geosGeometry);
-       if (*geomWKB == NULL)
+       if (*geomWKB == NULL) {
+               if (geomWKT_original)
+                       GDKfree(geomWKT);
                throw(MAL, "wkb.FromText", "geos2wkb failed");
+       }
 
        *len = (int) wkb_size((*geomWKB)->len);
 
@@ -2221,12 +2249,14 @@ wkbaFROMSTR_withSRID(char *fromStr, int 
        skipBytes += sizeof(int);
 
        *toArray = GDKmalloc(wkba_size(items));
+       if (*toArray == NULL)
+               return 0;
 
        for (i = 0; i < items; i++) {
                size_t parsedBytes;
                str err = wkbFROMSTR_withSRID(fromStr + skipBytes, len, 
&(*toArray)->data[i], srid, &parsedBytes);
                if (err != MAL_SUCCEED) {
-                       GDKfree(err);
+                       freeException(err);
                        return 0;
                }
                skipBytes += parsedBytes;
@@ -3263,7 +3293,7 @@ wkbMakeLineAggr(wkb **outWKB, bat *inBAT
                err = wkbFromWKB(outWKB, &aWKB);
                BBPunfix(inBAT->batCacheid);
                if (err) {
-                       GDKfree(err);
+                       freeException(err);
                        throw(MAL, "geom.MakeLine", MAL_MALLOC_FAIL);
                }
                return MAL_SUCCEED;
@@ -4214,7 +4244,7 @@ wkbUnionAggr(wkb **outWKB, bat *inBAT_id
                err = wkbFromWKB(outWKB, &aWKB);
                BBPunfix(inBAT->batCacheid);
                if (err) {
-                       GDKfree(err);
+                       freeException(err);
                        throw(MAL, "geom.Union", MAL_MALLOC_FAIL);
                }
                return MAL_SUCCEED;
@@ -4596,10 +4626,14 @@ wkbBox2D(mbr **box, wkb **point1, wkb **
        } else {
                //Assign the coordinates. Ensure that they are in correct order
                *box = GDKmalloc(sizeof(mbr));
-               (*box)->xmin = (float) (xmin < xmax ? xmin : xmax);
-               (*box)->ymin = (float) (ymin < ymax ? ymin : ymax);
-               (*box)->xmax = (float) (xmax > xmin ? xmax : xmin);
-               (*box)->ymax = (float) (ymax > ymin ? ymax : ymin);
+               if (*box == NULL) {
+                       err = createException(MAL, "geom.MakeBox2D", 
MAL_MALLOC_FAIL);
+               } else {
+                       (*box)->xmin = (float) (xmin < xmax ? xmin : xmax);
+                       (*box)->ymin = (float) (ymin < ymax ? ymin : ymax);
+                       (*box)->xmax = (float) (xmax > xmin ? xmax : xmin);
+                       (*box)->ymax = (float) (ymax > ymin ? ymax : ymin);
+               }
        }
        GEOSGeom_destroy(point1_geom);
        GEOSGeom_destroy(point2_geom);
@@ -5053,17 +5087,21 @@ wkbTOSTR(char **geomWKT, int *len, wkb *
        }
 
        if (wkt) {
-               if (*len < (int) dstStrLen + 1) {
+               if (*len < (int) dstStrLen + 1 || *geomWKT == NULL) {
                        *len = (int) dstStrLen + 1;
                        GDKfree(*geomWKT);
-                       *geomWKT = GDKmalloc(*len);
+                       if ((*geomWKT = GDKmalloc(*len)) == NULL) {
+                               GEOSFree(wkt);
+                               return 0;
+                       }
                }
                snprintf(*geomWKT, *len, "\"%s\"", wkt);
                GEOSFree(wkt);
        } else {
-               if (*len < 4) {
+               if (*len < 4 || *geomWKT == NULL) {
                        GDKfree(*geomWKT);
-                       *geomWKT = GDKmalloc(*len = 4);
+                       if ((*geomWKT = GDKmalloc(*len = 4)) == NULL)
+                               return 0;
                }
                strcpy(*geomWKT, "nil");
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to