]> git.ipfire.org Git - pakfire.git/commitdiff
util: Free pointer if it could not be reallocated
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 23 Oct 2024 11:54:50 +0000 (11:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 23 Oct 2024 11:54:50 +0000 (11:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index 4353b217a5ef144d9b379cdd229bddf0aeac452a..7bb432b7c5f28a7f4ed64cceb610ff93856ebeba 100644 (file)
 #include <pakfire/digest.h>
 #include <pakfire/pakfire.h>
 
+/*
+       This implementation of realloc frees the original buffer
+       if it could not be resized.
+*/
+static inline void* pakfire_realloc(void* p, size_t size) {
+       void* n = realloc(p, size);
+       if (!n) {
+               if (p)
+                       free(p);
+       }
+
+       return n;
+}
+
 int pakfire_path_exists(const char* path);
 int pakfire_path_match(const char* p, const char* s);
 time_t pakfire_path_age(const char* path);
index d884bb4dacb58d1bd09ef3891cce30ecee5e5a72..b8798036ba07d1f42d82942294553da231dd525d 100644 (file)
@@ -971,7 +971,7 @@ int pakfire_b64decode(struct pakfire_ctx* ctx, void** output, size_t* length,
                        ERR_error_string_n(ERR_get_error(), error, sizeof(error));
 
                        CTX_ERROR(ctx, "Could not read data: %s\n", error);
-                       r = 1;
+                       r = -EINVAL;
                        goto ERROR;
 
                // Break if no more data could be read
@@ -984,10 +984,10 @@ int pakfire_b64decode(struct pakfire_ctx* ctx, void** output, size_t* length,
                        *length += bytes_read;
 
                        // Allocate an output buffer
-                       p = realloc(p, *length);
+                       p = pakfire_realloc(p, *length);
                        if (!p) {
                                CTX_ERROR(ctx, "Could not allocate buffer: %m\n");
-                               r = 1;
+                               r = -errno;
                                goto ERROR;
                        }