]> git.ipfire.org Git - pakfire.git/commitdiff
util: Fix double free in base64 encoder
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 6 Oct 2023 14:37:02 +0000 (14:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 6 Oct 2023 14:37:02 +0000 (14:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/util.c

index c06bf59ee7b38d8f4624ea7526d7fb9a431c54e0..8f3c68a451268290b701b0ab29fc7cc4a8cad871 100644 (file)
@@ -926,7 +926,7 @@ int pakfire_b64encode(struct pakfire* pakfire, char** output,
        char error[OPENSSL_ERROR_MAX];
        BIO* b64 = NULL;
        BIO* bio = NULL;
-       char* p = NULL;
+       const char* p = NULL;
        int r;
 
        // Initialize the base64 encoder
@@ -939,9 +939,6 @@ int pakfire_b64encode(struct pakfire* pakfire, char** output,
                goto ERROR;
        }
 
-       // Disable line breaks and a trailing newline
-       BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
-
        // Initialize a memory buffer
        bio = BIO_new(BIO_s_mem());
        if (!bio) {
@@ -953,10 +950,13 @@ int pakfire_b64encode(struct pakfire* pakfire, char** output,
        }
 
        // Connect both things
-       b64 = BIO_push(b64, bio);
+       bio = BIO_push(b64, bio);
+
+       // Disable line breaks and a trailing newline
+       BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
 
        // Write the input
-       r = BIO_write(b64, buffer, length);
+       r = BIO_write(bio, buffer, length);
        if (r < 1) {
                ERR_error_string_n(ERR_get_error(), error, sizeof(error));
 
@@ -966,10 +966,10 @@ int pakfire_b64encode(struct pakfire* pakfire, char** output,
        }
 
        // Flush
-       BIO_flush(b64);
+       BIO_flush(bio);
 
        // Fetch a pointer to the output and determine its length
-       const size_t l = BIO_get_mem_data(b64, &p);
+       const size_t l = BIO_get_mem_data(bio, &p);
 
        // Copy the output to the heap
        *output = strndup(p, l);
@@ -983,12 +983,8 @@ int pakfire_b64encode(struct pakfire* pakfire, char** output,
        r = 0;
 
 ERROR:
-       if (p)
-               free(p);
        if (bio)
-               BIO_free(bio);
-       if (b64)
-               BIO_free(b64);
+               BIO_free_all(bio);
 
        return r;
 }