From d44eb2ffa4a50b9c5c6a269bcfd7cd946c27b045 Mon Sep 17 00:00:00 2001 From: Emil Velikov Date: Wed, 4 Jun 2025 17:16:25 +0100 Subject: [PATCH] Use explicit ENOMEM when {m,re}alloc fails 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 Link: https://github.com/kmod-project/kmod/pull/368 Signed-off-by: Lucas De Marchi --- libkmod/libkmod-elf.c | 13 ++++++------- libkmod/libkmod-file-xz.c | 2 +- libkmod/libkmod-file-zlib.c | 2 +- libkmod/libkmod-file-zstd.c | 2 +- shared/hash.c | 4 ++-- tools/opt.c | 2 +- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/libkmod/libkmod-elf.c b/libkmod/libkmod-elf.c index 23d27f0..52b0433 100644 --- a/libkmod/libkmod-elf.c +++ b/libkmod/libkmod-elf.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -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; diff --git a/libkmod/libkmod-file-xz.c b/libkmod/libkmod-file-xz.c index cafe828..432a647 100644 --- a/libkmod/libkmod-file-xz.c +++ b/libkmod/libkmod-file-xz.c @@ -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); diff --git a/libkmod/libkmod-file-zlib.c b/libkmod/libkmod-file-zlib.c index db9fc8e..54d6882 100644 --- a/libkmod/libkmod-file-zlib.c +++ b/libkmod/libkmod-file-zlib.c @@ -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; diff --git a/libkmod/libkmod-file-zstd.c b/libkmod/libkmod-file-zstd.c index 8f09459..bd0c8fd 100644 --- a/libkmod/libkmod-file-zstd.c +++ b/libkmod/libkmod-file-zstd.c @@ -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; } diff --git a/shared/hash.c b/shared/hash.c index d0e7f86..6009004 100644 --- a/shared/hash.c +++ b/shared/hash.c @@ -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; } diff --git a/tools/opt.c b/tools/opt.c index 944209b..18a519d 100644 --- a/tools/opt.c +++ b/tools/opt.c @@ -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");