Changeset: e9577e693866 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/e9577e693866
Modified Files:
        gdk/gdk_string.c
        geom/monetdb5/geom.c
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/str.c
        monetdb5/modules/atoms/streams.c
        monetdb5/modules/mal/calc.c
        monetdb5/modules/mal/inspect.c
        monetdb5/modules/mal/mal_io.c
        monetdb5/modules/mal/tablet.c
Branch: Dec2025
Log Message:

Unlike (GDK)realloc, ma_realloc does not preserve old buffer in case of failure.


diffs (truncated from 642 to 300 lines):

diff --git a/gdk/gdk_string.c b/gdk/gdk_string.c
--- a/gdk/gdk_string.c
+++ b/gdk/gdk_string.c
@@ -6887,11 +6887,11 @@ convertcase(allocator *ma, char **restri
                                 * terminating NUL */
                                size_t newlen = bl + 1024;
                                dst = ma_realloc(ma, *buf, newlen, bl);
+                               *buf = (char *) dst;
                                if (dst == NULL) {
-                                       *buflen = bl;
+                                       *buflen = 0;
                                        return GDK_FAIL;
                                }
-                               *buf = (char *) dst;
                                bl = newlen;
                                bl5 = bl - 5;
                        }
@@ -6935,11 +6935,11 @@ convertcase(allocator *ma, char **restri
        if (dstoff + 1 > bl) {
                size_t newlen = dstoff + 1;
                dst = ma_realloc(ma, *buf, newlen, bl);
+               *buf = (char *) dst;
                if (dst == NULL) {
-                       *buflen = bl;
+                       *buflen = 0;
                        return GDK_FAIL;
                }
-               *buf = (char *) dst;
                bl = newlen;
        }
        dst[dstoff] = '\0';
@@ -9726,11 +9726,11 @@ GDKasciify(allocator *ma, char **restric
                                 * bytes plus terminating NUL */
                                size_t newlen = bl + 1024;
                                dst = ma_realloc(ma, *buf, newlen, bl);
+                               *buf = (char *) dst;
                                if (dst == NULL) {
-                                       *buflen = bl;
+                                       *buflen = 0;
                                        return GDK_FAIL;
                                }
-                               *buf = (char *) dst;
                                bl = newlen;
                                bl8 = bl - 8;
                        }
@@ -9756,11 +9756,11 @@ GDKasciify(allocator *ma, char **restric
        if (dstoff + 1 > bl) {
                size_t newlen = dstoff + 1;
                dst = ma_realloc(ma, *buf, newlen, bl);
+               *buf = (char *) dst;
                if (dst == NULL) {
-                       *buflen = bl;
+                       *buflen = 0;
                        return GDK_FAIL;
                }
-               *buf = (char *) dst;
                bl = newlen;
        }
        dst[dstoff] = '\0';
diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c
--- a/geom/monetdb5/geom.c
+++ b/geom/monetdb5/geom.c
@@ -5085,17 +5085,14 @@ wkbContains_point_bat(Client ctx, bat *o
                sscanf(subtoken, "%lf %lf", &vert_x[nvert], &vert_y[nvert]);
                nvert++;
                if ((nvert % POLY_NUM_VERT) == 0) {
-                       double *tmp;
-                       tmp = ma_realloc(ma, vert_x, nvert * 2 * 
sizeof(double), nvert);
-                       if (tmp == NULL) {
+                       vert_x = ma_realloc(ma, vert_x, nvert * 2 * 
sizeof(double), nvert);
+                       if (vert_x == NULL) {
                                throw(MAL, "geom.Contains", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       vert_x = tmp;
-                       tmp = ma_realloc(ma, vert_y, nvert * 2 * 
sizeof(double), nvert);
-                       if (tmp == NULL) {
+                       vert_y = ma_realloc(ma, vert_y, nvert * 2 * 
sizeof(double), nvert);
+                       if (vert_y == NULL) {
                                throw(MAL, "geom.Contains", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       vert_y = tmp;
                }
        }
 
@@ -5132,40 +5129,32 @@ wkbContains_point_bat(Client ctx, bat *o
                        sscanf(subtoken, "%lf %lf", &holes_x[nholes][nhole], 
&holes_y[nholes][nhole]);
                        nhole++;
                        if ((nhole % POLY_NUM_VERT) == 0) {
-                               double *tmp;
-                               tmp = ma_realloc(ma, holes_x[nholes], nhole * 2 
* sizeof(double), nhole);
-                               if (tmp == NULL) {
+                               holes_x[nholes] = ma_realloc(ma, 
holes_x[nholes], nhole * 2 * sizeof(double), nhole);
+                               if (holes_x[nholes] == NULL) {
                                        throw(MAL, "geom.Contains", 
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                                }
-                               holes_x[nholes] = tmp;
-                               tmp = ma_realloc(ma, holes_y[nholes], nhole * 2 
* sizeof(double), nhole);
-                               if (tmp == NULL) {
+                               holes_y[nholes] = ma_realloc(ma, 
holes_y[nholes], nhole * 2 * sizeof(double), nhole);
+                               if (holes_y[nholes] == NULL) {
                                        throw(MAL, "geom.Contains", 
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                                }
-                               holes_y[nholes] = tmp;
                        }
                }
 
                holes_n[nholes] = nhole;
                nholes++;
                if ((nholes % POLY_NUM_HOLE) == 0) {
-                       double **tmp;
-                       int *itmp;
-                       tmp = ma_realloc(ma, holes_x, nholes * 2 * 
sizeof(double *), nholes);
-                       if (tmp == NULL) {
+                       holes_x = ma_realloc(ma, holes_x, nholes * 2 * 
sizeof(double *), nholes);
+                       if (holes_x == NULL) {
                                throw(MAL, "geom.Contains", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       holes_x = tmp;
-                       tmp = ma_realloc(ma, holes_y, nholes * 2 * 
sizeof(double *), nholes);
-                       if (tmp == NULL) {
+                       holes_y = ma_realloc(ma, holes_y, nholes * 2 * 
sizeof(double *), nholes);
+                       if (holes_y == NULL) {
                                throw(MAL, "geom.Contains", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       holes_y = tmp;
-                       itmp = ma_realloc(ma, holes_n, nholes * 2 * 
sizeof(int), nholes);
-                       if (itmp == NULL) {
+                       holes_n = ma_realloc(ma, holes_n, nholes * 2 * 
sizeof(int), nholes);
+                       if (holes_n == NULL) {
                                throw(MAL, "geom.Contains", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       holes_n = itmp;
                }
                token = strtok_r(NULL, ")", &saveptr1);
        }
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -102,19 +102,16 @@ JSONnewtree(allocator *ma, int initsize)
 static int
 JSONnew(JSON *js)
 {
-       JSONterm *term;
-
        size_t osz = sizeof(JSONterm) * js->size;
        if (js->free == js->size) {
                size_t nsz = sizeof(JSONterm) * (js->size + 8);
-               term = ma_realloc(js->ma, js->elm, nsz, osz);
-               if (term == NULL) {
+               js->elm = ma_realloc(js->ma, js->elm, nsz, osz);
+               if (js->elm == NULL) {
                        js->error = createException(MAL, "json.new",
                                                                                
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                        return js->free - 1;
                }
-               js->elm = term;
-               memset(term + js->size, 0, 8 * sizeof(JSONterm));
+               memset(js->elm + js->size, 0, 8 * sizeof(JSONterm));
                js->size += 8;
        }
        return js->free++;
@@ -343,13 +340,12 @@ JSONdumpInternal(Client ctx, const JSON 
                        do {
                                buflen += 1024;
                        } while (datlen + depth * 4 + 512 > buflen);
-                       char *newbuf = ma_realloc(ta, buffer, buflen, osz);
-                       if (newbuf == NULL) {
+                       buffer = ma_realloc(ta, buffer, buflen, osz);
+                       if (buffer == NULL) {
                                BBPreclaim(bn);
                                ma_close(&ta_state);
                                return NULL;
                        }
-                       buffer = newbuf;
                }
                datlen += snprintf(buffer + datlen, buflen - datlen, "%*s", 
depth * 4,
                                                   "");
@@ -389,13 +385,12 @@ JSONdumpInternal(Client ctx, const JSON 
                        size_t osz = buflen;
                        if (datlen + 10 > buflen) {
                                buflen += 1024;
-                               char *newbuf = ma_realloc(ta, buffer, buflen, 
osz);
-                               if (newbuf == NULL) {
+                               buffer = ma_realloc(ta, buffer, buflen, osz);
+                               if (buffer == NULL) {
                                        BBPreclaim(bn);
                                        ma_close(&ta_state);
                                        return NULL;
                                }
-                               buffer = newbuf;
                        }
                        datlen += snprintf(buffer + datlen, buflen - datlen, 
"%d ", i);
                }
@@ -405,13 +400,12 @@ JSONdumpInternal(Client ctx, const JSON 
                                do {
                                        buflen += 1024;
                                } while (datlen + 10 + je->namelen > buflen);
-                               char *newbuf = ma_realloc(ta, buffer, buflen, 
osz);
-                               if (newbuf == NULL) {
+                               buffer = ma_realloc(ta, buffer, buflen, osz);
+                               if (buffer == NULL) {
                                        BBPreclaim(bn);
                                        ma_close(&ta_state);
                                        return NULL;
                                }
-                               buffer = newbuf;
                        }
                        datlen += snprintf(buffer + datlen, buflen - datlen, 
"%.*s : ",
                                                           (int) je->namelen, 
je->name);
@@ -422,13 +416,12 @@ JSONdumpInternal(Client ctx, const JSON 
                                do {
                                        buflen += 1024;
                                } while (datlen + 10 + je->valuelen > buflen);
-                               char *newbuf = ma_realloc(ta, buffer, buflen, 
osz);
-                               if (newbuf == NULL) {
+                               buffer = ma_realloc(ta, buffer, buflen, osz);
+                               if (buffer == NULL) {
                                        BBPreclaim(bn);
                                        ma_close(&ta_state);
                                        return NULL;
                                }
-                               buffer = newbuf;
                        }
                        datlen += snprintf(buffer + datlen, buflen - datlen, 
"%.*s",
                                                           (int) je->valuelen, 
je->value);
@@ -1651,6 +1644,12 @@ static str
 JSONplaintext(allocator *ma, char **r, size_t *l, size_t *ilen, const JSON 
*jt, int idx, const char *sep,
                          size_t sep_len)
 {
+       /* *r point to next available position to write to;
+        * *l is how much space is left in the buffer;
+        * *ilen is the total size of the buffer;
+        * hence *r + *l is the end of the buffer
+        * and *r + *l - *ilen (or *r - (*ilen - *l)) is the start
+        */
        int i;
        size_t j, next_len, next_concat_len;
        unsigned int u;
@@ -1687,16 +1686,16 @@ JSONplaintext(allocator *ma, char **r, s
                next_len = jt->elm[idx].valuelen;
                next_concat_len = next_len - 2 + sep_len + 1;
                if (*l < next_concat_len) {
-                       size_t prev_ilen = *ilen, prev_l = *l;
-                       char *p = *r - (prev_ilen - prev_l), *nr;
+                       size_t prev_ilen = *ilen;
+                       char *p = *r + *l - prev_ilen; /* start of buffer */
 
-                       *ilen = (prev_ilen * 2) + next_concat_len;      /* make 
sure sep_len + 1 is always included */
-                       if (!(nr = ma_realloc(ma, p, *ilen, prev_ilen))) {
-                               *r = p;
+                       *ilen = prev_ilen * 2 + next_concat_len;        /* make 
sure sep_len + 1 is always included */
+                       if (!(p = ma_realloc(ma, p, *ilen, prev_ilen))) {
+                               *r = NULL;
                                throw(MAL, "JSONplaintext", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       *r = nr + (prev_ilen - prev_l);
-                       *l = *ilen - prev_ilen + prev_l;
+                       *r = p + prev_ilen - *l;
+                       *l += *ilen - prev_ilen;
                }
                assert(next_len >= 2);
                next_len--;
@@ -1779,16 +1778,16 @@ JSONplaintext(allocator *ma, char **r, s
                next_len = jt->elm[idx].valuelen;
                next_concat_len = next_len + sep_len + 1;
                if (*l < next_concat_len) {
-                       size_t prev_ilen = *ilen, prev_l = *l;
-                       char *p = *r - (prev_ilen - prev_l), *nr;
+                       size_t prev_ilen = *ilen;
+                       char *p = *r + *l - prev_ilen;
 
-                       *ilen = (prev_ilen * 2) + next_concat_len;      /* make 
sure sep_len + 1 is always included */
-                       if (!(nr = ma_realloc(ma, p, *ilen, prev_ilen))) {
-                               *r = p;
+                       *ilen = prev_ilen * 2 + next_concat_len;        /* make 
sure sep_len + 1 is always included */
+                       if (!(p = ma_realloc(ma, p, *ilen, prev_ilen))) {
+                               *r = NULL;
                                throw(MAL, "JSONplaintext", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
-                       *r = nr + (prev_ilen - prev_l);
-                       *l = *ilen - prev_ilen + prev_l;
+                       *r = p + prev_ilen - *l;
+                       *l += *ilen - prev_ilen;
                }
                memcpy(*r, jt->elm[idx].value, next_len);
                *l -= next_len;
@@ -2294,7 +2293,7 @@ JSONrenderRowObject(allocator *ma, BAT *
                                        BUN idx)
 {
        int i, tpe;
-       char *row, *row2, *name = 0, *val = 0;
+       char *row, *name = 0, *val = 0;
        size_t len, lim, l;
        void *p;
        BATiter bi;
@@ -2325,11 +2324,10 @@ JSONrenderRowObject(allocator *ma, BAT *
                size_t osz = lim;
                while (l > lim - len)
                        lim += BUFSIZ;
-               row2 = ma_realloc(ma, row, lim, osz);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to