]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Use explicit ENOMEM when {m,re}alloc fails
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 4 Jun 2025 16:16:25 +0000 (17:16 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Wed, 11 Jun 2025 13:03:26 +0000 (08:03 -0500)
Currently our codebase has a mix of explicitly using ENOMEM and
propagating the errno... The latter of which is guaranteed to be ENOMEM
as documented in the manual pages.

Consolidate on ENOMEM, both for consistency sake and to remove a few
bytes off our binaries ;-)

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-elf.c
libkmod/libkmod-file-xz.c
libkmod/libkmod-file-zlib.c
libkmod/libkmod-file-zstd.c
shared/hash.c
tools/opt.c

index 23d27f027340bc8c2e7651ce831adf0bf1e7e471..52b04339c36ea604a1f88ec1ae33392e3fad1622 100644 (file)
@@ -6,7 +6,6 @@
 #include <assert.h>
 #include <elf.h>
 #include <endian.h>
-#include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
@@ -485,7 +484,7 @@ int kmod_elf_get_modinfo_strings(const struct kmod_elf *elf, char ***array)
 
        *array = a = malloc(total_size);
        if (*array == NULL)
-               return -errno;
+               return -ENOMEM;
 
        s = (char *)(a + count + 1);
        memcpy(s, strings, size);
@@ -561,7 +560,7 @@ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion
 
        *array = a = malloc(sizeof(struct kmod_modversion) * count);
        if (*array == NULL)
-               return -errno;
+               return -ENOMEM;
 
        for (i = 0, off = sec_off; i < count; i++, off += verlen) {
                uint64_t crc = elf_get_uint(elf, off, crclen);
@@ -667,7 +666,7 @@ int kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags, const void **
 
        changed = memdup(elf->memory, elf->size);
        if (changed == NULL)
-               return -errno;
+               return -ENOMEM;
 
        ELFDBG(elf, "copied memory to allow writing.\n");
 
@@ -743,7 +742,7 @@ static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,
 
        *array = a = malloc(total_size);
        if (*array == NULL)
-               return -errno;
+               return -ENOMEM;
 
        last = 0;
        for (i = 0, count = 0; i < size; i++) {
@@ -882,7 +881,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar
 
        *array = a = malloc(sizeof(struct kmod_modversion) * count);
        if (*array == NULL)
-               return -errno;
+               return -ENOMEM;
 
        count = 0;
        str_off = str_sec_off;
@@ -1154,7 +1153,7 @@ int kmod_elf_get_dependency_symbols(const struct kmod_elf *elf,
        if (*array == NULL) {
                free(visited_versions);
                free(symcrcs);
-               return -errno;
+               return -ENOMEM;
        }
 
        count = 0;
index cafe828c2bd7c17752cea76fd54b0d08b2e5b4a0..432a6473016ed3e751dc5feb2ec62878a899e6f7 100644 (file)
@@ -95,7 +95,7 @@ static int xz_uncompress(lzma_stream *strm, struct kmod_file *file)
                        size_t write_size = BUFSIZ - strm->avail_out;
                        char *tmp = realloc(p, total + write_size);
                        if (tmp == NULL) {
-                               ret = -errno;
+                               ret = -ENOMEM;
                                goto out;
                        }
                        memcpy(tmp + total, out_buf, write_size);
index db9fc8e38191a9ee8878987a56d86921fe300f46..54d688218c2458e6db7690f0032c81e5ee9ed6f9 100644 (file)
@@ -76,7 +76,7 @@ int kmod_file_load_zlib(struct kmod_file *file)
                if (did == total) {
                        void *tmp = realloc(p, total + READ_STEP);
                        if (tmp == NULL) {
-                               ret = -errno;
+                               ret = -ENOMEM;
                                goto error;
                        }
                        total += READ_STEP;
index 8f0945976e9a129519db3d32e129e0118c728660..bd0c8fd4060f0cd216cce4b96212ee8b48ba260d 100644 (file)
@@ -93,7 +93,7 @@ int kmod_file_load_zstd(struct kmod_file *file)
        dst_size = frame_size;
        dst_buf = malloc(dst_size);
        if (dst_buf == NULL) {
-               ret = -errno;
+               ret = -ENOMEM;
                goto out;
        }
 
index d0e7f86d727166437a27a9ee79cbc8a5f7d847cd..600900494222bd2ccee84f9ba67563ee00b751b7 100644 (file)
@@ -140,7 +140,7 @@ int hash_add(struct hash *hash, const char *key, const void *value)
                size_t size = new_total * sizeof(struct hash_entry);
                struct hash_entry *tmp = realloc(bucket->entries, size);
                if (tmp == NULL)
-                       return -errno;
+                       return -ENOMEM;
                bucket->entries = tmp;
                bucket->total = new_total;
        }
@@ -183,7 +183,7 @@ int hash_add_unique(struct hash *hash, const char *key, const void *value)
                size_t size = new_total * sizeof(struct hash_entry);
                struct hash_entry *tmp = realloc(bucket->entries, size);
                if (tmp == NULL)
-                       return -errno;
+                       return -ENOMEM;
                bucket->entries = tmp;
                bucket->total = new_total;
        }
index 944209bdef69a5d10316745a80950f31bc2941fb..18a519d97859a8f619ace5347bc6830795ea0008 100644 (file)
@@ -32,7 +32,7 @@ int options_from_array(char **args, int nargs, char **output)
 
                tmp = realloc(opts, optslen + len + qlen + 2);
                if (!tmp) {
-                       err = -errno;
+                       err = -ENOMEM;
                        free(opts);
                        opts = NULL;
                        ERR("could not gather module options: out-of-memory\n");