Changeset: 0b4e3eabc883 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/0b4e3eabc883
Modified Files:
        gdk/gdk_bbp.c
Branch: Sep2022
Log Message:

If there happen to BAT options in BBP.dir, don't return pointer into stack.


diffs (132 lines):

diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -632,6 +632,11 @@ heapinit(BAT *b, const char *buf,
  * in the structure pointed to by bn and extra information through the
  * other pointers; this function does not allocate any memory; return 0
  * on end of file, 1 on success, and -1 on failure */
+/* set to true during initialization, else always false; if false, do
+ * not return any options (set pointer to NULL as if there aren't any);
+ * if true and there are options, return them in freshly allocated
+ * memory through *options */
+static bool return_options = false;
 int
 BBPreadBBPline(FILE *fp, unsigned bbpversion, int *lineno, BAT *bn,
 #ifdef GDKLIBRARY_HASHASH
@@ -739,11 +744,20 @@ BBPreadBBPline(FILE *fp, unsigned bbpver
        }
        nread += n;
 
-       if (buf[nread] != '\n' && buf[nread] != ' ') {
+       if (nread >= sizeof(buf) || (buf[nread] != '\n' && buf[nread] != ' ')) {
                TRC_CRITICAL(GDK, "invalid format for BBP.dir on line %d", 
*lineno);
                return -1;
        }
-       *options = (buf[nread] == ' ') ? buf + nread + 1 : NULL;
+       if (options) {
+               if (return_options && buf[nread] == ' ') {
+                       if ((*options = GDKstrdup(buf + nread + 1)) == NULL) {
+                               TRC_CRITICAL(GDK, "GDKstrdup failed\n");
+                               return -1;
+                       }
+               } else {
+                       *options = NULL;
+               }
+       }
        return 1;
 }
 
@@ -760,6 +774,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
 #endif
 
        /* read the BBP.dir and insert the BATs into the BBP */
+       return_options = true;
        for (;;) {
                BAT b;
                Heap h;
@@ -790,6 +805,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                        *hashbats = hbats;
                        *nhashbats = nhbats;
 #endif
+                       return_options = false;
                        return GDK_SUCCEED;
                case 1:
                        /* successfully read an entry */
@@ -800,17 +816,21 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                }
 
                if (b.batCacheid >= N_BBPINIT * BBPINIT) {
+                       GDKfree(options);
                        TRC_CRITICAL(GDK, "bat ID (%d) too large to accommodate 
(max %d), on line %d.", b.batCacheid, N_BBPINIT * BBPINIT - 1, lineno);
                        goto bailout;
                }
 
                if (b.batCacheid >= (bat) ATOMIC_GET(&BBPsize)) {
                        if ((bat) ATOMIC_GET(&BBPsize) + 1 >= BBPlimit &&
-                           BBPextend(false, b.batCacheid + 1) != GDK_SUCCEED)
+                           BBPextend(false, b.batCacheid + 1) != GDK_SUCCEED) {
+                               GDKfree(options);
                                goto bailout;
+                       }
                        ATOMIC_SET(&BBPsize, b.batCacheid + 1);
                }
                if (BBP_desc(b.batCacheid) != NULL) {
+                       GDKfree(options);
                        TRC_CRITICAL(GDK, "duplicate entry in BBP.dir (ID = "
                                     "%d) on line %d.", b.batCacheid, lineno);
                        goto bailout;
@@ -821,6 +841,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                        assert(bbpversion <= GDKLIBRARY_HASHASH);
                        bat *sb = GDKrealloc(hbats, ++nhbats * sizeof(bat));
                        if (sb == NULL) {
+                               GDKfree(options);
                                goto bailout;
                        }
                        hbats = sb;
@@ -833,20 +854,13 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                if ((bn = GDKzalloc(sizeof(BAT))) == NULL ||
                    (hn = GDKzalloc(sizeof(Heap))) == NULL) {
                        GDKfree(bn);
+                       GDKfree(options);
                        TRC_CRITICAL(GDK, "cannot allocate memory for BAT.");
                        goto bailout;
                }
                *bn = b;
                *hn = h;
                bn->theap = hn;
-               if (options &&
-                   (options = GDKstrdup(options)) == NULL) {
-                       GDKfree(hn);
-                       GDKfree(bn);
-                       PROPdestroy_nolock(&b);
-                       TRC_CRITICAL(GDK, "GDKstrdup failed\n");
-                       goto bailout;
-               }
                if (b.tvheap) {
                        Heap *vhn;
                        assert(b.tvheap == &vh);
@@ -912,6 +926,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
        }
 
   bailout:
+       return_options = false;
 #ifdef GDKLIBRARY_HASHASH
        GDKfree(hbats);
 #endif
@@ -3711,7 +3726,6 @@ BBPcheckBBPdir(bool subcommit)
                        .theap = &h,
                        .tvheap = &vh,
                };
-               char *options;
                char filename[sizeof(BBP_physical(0))];
                char batname[129];
 #ifdef GDKLIBRARY_HASHASH
@@ -3722,7 +3736,7 @@ BBPcheckBBPdir(bool subcommit)
 #ifdef GDKLIBRARY_HASHASH
                                       &hashash,
 #endif
-                                      batname, filename, &options)) {
+                                      batname, filename, NULL)) {
                case 0:
                        /* end of file */
                        fclose(fp);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to