]> git.ipfire.org Git - pakfire.git/commitdiff
strings: Copy strings a lot when appending to an array
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Jan 2025 15:48:32 +0000 (15:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Jan 2025 15:48:32 +0000 (15:48 +0000)
The static analyzer thinks we are leaking memory, and so we have to make
things a little bit easier for it.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c

index 7e59d09af940980d250f1dab50ccc5309c366183..19fe4aa85bf3bc5fcc0bf53bee6c792d2b83aa32 100644 (file)
@@ -404,42 +404,37 @@ void pakfire_strings_free(char** array) {
        free(array);
 }
 
-static int __pakfire_strings_append(char*** array, char* s) {
+int pakfire_strings_append(char*** array, const char* s) {
        if (!array)
                return -EINVAL;
 
+       // Copy the string
+       char* p = strdup(s);
+       if (!p)
+               goto ERROR;
+
        // Fetch the length
        size_t length = pakfire_strings_length(*array);
 
        // Resize the array
        *array = reallocarray(*array, length + 2, sizeof(**array));
        if (!*array)
-               return -errno;
+               goto ERROR;
 
        // Store the string
-       (*array)[length++] = s;
+       (*array)[length++] = p;
 
        // Terminate the array
        (*array)[length] = NULL;
 
        // Return the total length of the array
        return length;
-}
-
-int pakfire_strings_append(char*** array, const char* s) {
-       int r;
 
-       // Copy the string
-       char* p = strdup(s);
-       if (!p)
-               return -errno;
-
-       // Append it to the array
-       r = __pakfire_strings_append(array, p);
-       if (r < 0)
+ERROR:
+       if (p)
                free(p);
 
-       return r;
+       return -errno;
 }
 
 int pakfire_strings_appendf(char*** array, const char* format, ...) {
@@ -457,9 +452,8 @@ int pakfire_strings_appendf(char*** array, const char* format, ...) {
                return -errno;
 
        // Append it to the array
-       r = __pakfire_strings_append(array, p);
-       if (r < 0)
-               free(p);
+       r = pakfire_strings_append(array, p);
+       free(p);
 
        return r;
 }