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 <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>
This commit is contained in:
Emil Velikov 2025-06-04 17:16:25 +01:00 committed by Lucas De Marchi
parent 1eeb00f776
commit d44eb2ffa4
6 changed files with 12 additions and 13 deletions

View 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;

View 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);

View 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;

View 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;
}

View 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;
}

View 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");