Changeset: a8f7fe83bdae for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a8f7fe83bdae
Modified Files:
        gdk/gdk_string.c
Branch: Jun2023
Log Message:

Fix some potential leaks and simplify the code a little.


diffs (140 lines):

diff --git a/gdk/gdk_string.c b/gdk/gdk_string.c
--- a/gdk/gdk_string.c
+++ b/gdk/gdk_string.c
@@ -769,32 +769,30 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
        str *restrict astrings = NULL;
        BATiter bi, bis = (BATiter) {0};
        BAT *bn = NULL;
-       gdk_return rres = GDK_SUCCEED;
+       gdk_return rres = GDK_FAIL;
 
        /* exactly one of bnp and pt must be NULL, the other non-NULL */
        assert((bnp == NULL) != (pt == NULL));
        /* if pt not NULL, only a single group allowed */
        assert(pt == NULL || ngrp == 1);
 
-       bi = bat_iterator(b);
-       if (sep)
-               bis = bat_iterator(sep);
-       else
-               separator_length = strlen(separator);
-
        if (bnp) {
-               if ((bn = COLnew(min, TYPE_str, ngrp, TRANSIENT)) == NULL) {
-                       rres = GDK_FAIL;
-                       goto finish;
-               }
+               if ((bn = COLnew(min, TYPE_str, ngrp, TRANSIENT)) == NULL)
+                       return GDK_FAIL;
                *bnp = bn;
        }
 
+       bi = bat_iterator(b);
+               bis = bat_iterator(sep);
+       if (separator)
+               separator_length = strlen(separator);
+
        if (ngrp == 1) {
                size_t offset = 0, single_length = 0;
                bool empty = true;
 
                if (separator) {
+                       assert(sep == NULL);
                        CAND_LOOP_IDX(ci, i) {
                                p = canditer_next(ci) - seqb;
                                const char *s = BUNtvar(bi, p);
@@ -843,8 +841,8 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
 
                        if ((single_str = GDKmalloc(single_length + 1)) == 
NULL) {
                                bat_iterator_end(&bi);
-                               if (sep)
-                                       bat_iterator_end(&bis);
+                               bat_iterator_end(&bis);
+                               BBPreclaim(bn);
                                return GDK_FAIL;
                        }
                        empty = true;
@@ -888,8 +886,8 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
                                if (BUNappend(bn, single_str, false) != 
GDK_SUCCEED) {
                                        GDKfree(single_str);
                                        bat_iterator_end(&bi);
-                                       if (sep)
-                                               bat_iterator_end(&bis);
+                                       bat_iterator_end(&bis);
+                                       BBPreclaim(bn);
                                        return GDK_FAIL;
                                }
                        } else {
@@ -901,21 +899,19 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
                } else if (bn) {
                        if (BUNappend(bn, str_nil, false) != GDK_SUCCEED) {
                                bat_iterator_end(&bi);
-                               if (sep)
-                                       bat_iterator_end(&bis);
+                               bat_iterator_end(&bis);
+                               BBPreclaim(bn);
                                return GDK_FAIL;
                        }
                } else {
                        if (VALinit(pt, TYPE_str, str_nil) == NULL) {
                                bat_iterator_end(&bi);
-                               if (sep)
-                                       bat_iterator_end(&bis);
+                               bat_iterator_end(&bis);
                                return GDK_FAIL;
                        }
                }
                bat_iterator_end(&bi);
-               if (sep)
-                       bat_iterator_end(&bis);
+               bat_iterator_end(&bis);
                return GDK_SUCCEED;
        } else {
                /* first used to calculated the total length of
@@ -925,7 +921,6 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
                if (sep)
                        lastseplength = GDKzalloc(ngrp * 
sizeof(*lastseplength));
                if (lengths == NULL || astrings == NULL || (sep && 
lastseplength == NULL)) {
-                       rres = GDK_FAIL;
                        goto finish;
                }
                /* at first, set astrings[i] to str_nil, then for each
@@ -985,7 +980,6 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
                        for (i = 0; i < ngrp; i++) {
                                if (astrings[i] == NULL) {
                                        if ((astrings[i] = GDKmalloc(lengths[i] 
+ 1 - separator_length)) == NULL) {
-                                               rres = GDK_FAIL;
                                                goto finish;
                                        }
                                        astrings[i][0] = 0;
@@ -998,7 +992,6 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
                        for (i = 0; i < ngrp; i++) {
                                if (astrings[i] == NULL) {
                                        if ((astrings[i] = GDKmalloc(lengths[i] 
+ 1 - lastseplength[i])) == NULL) {
-                                               rres = GDK_FAIL;
                                                goto finish;
                                        }
                                        astrings[i][0] = 0;
@@ -1058,20 +1051,18 @@ concat_strings(BAT **bnp, ValPtr pt, BAT
                        if (astrings[i]) {
                                astrings[i][lengths[i]] = '\0';
                                if (BUNappend(bn, astrings[i], false) != 
GDK_SUCCEED) {
-                                       rres = GDK_FAIL;
                                        goto finish;
                                }
                        } else if (BUNappend(bn, str_nil, false) != 
GDK_SUCCEED) {
-                               rres = GDK_FAIL;
                                goto finish;
                        }
                }
+               rres = GDK_SUCCEED;
        }
 
   finish:
        bat_iterator_end(&bi);
-       if (sep)
-               bat_iterator_end(&bis);
+       bat_iterator_end(&bis);
        if (has_nils)
                *has_nils = nils;
        GDKfree(lengths);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to