From: Emil Velikov Date: Wed, 7 May 2025 17:58:11 +0000 (+0100) Subject: libkmod: return the errno from kmod_elf_strip() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=132eb4ca83b291c81ed1ba31fd5120fd6944451d;p=thirdparty%2Fkmod.git libkmod: return the errno from kmod_elf_strip() Rework the function signature and return the error code instead of the stripped module. Thus we no longer explicitly set errno. Reference: https://github.com/kmod-project/kmod/issues/244 Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/346 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-elf.c b/libkmod/libkmod-elf.c index f215f1ee..4590543c 100644 --- a/libkmod/libkmod-elf.c +++ b/libkmod/libkmod-elf.c @@ -666,7 +666,7 @@ static int elf_strip_vermagic(const struct kmod_elf *elf, uint8_t *changed) return -ENODATA; } -const void *kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags) +int kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags, const void **stripped) { uint8_t *changed; int err = 0; @@ -675,30 +675,27 @@ const void *kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags) changed = memdup(elf->memory, elf->size); if (changed == NULL) - return NULL; + return -errno; ELFDBG(elf, "copied memory to allow writing.\n"); if (flags & KMOD_INSERT_FORCE_MODVERSION) { err = elf_strip_versions_section(elf, changed); - if (err < 0) { - errno = -err; + if (err < 0) goto fail; - } } if (flags & KMOD_INSERT_FORCE_VERMAGIC) { err = elf_strip_vermagic(elf, changed); - if (err < 0) { - errno = -err; + if (err < 0) goto fail; - } } - return changed; + *stripped = changed; + return 0; fail: free(changed); - return NULL; + return err; } static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf, diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 4706071d..8d322b65 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -159,7 +159,7 @@ _must_check_ _nonnull_all_ int kmod_elf_get_modinfo_strings(const struct kmod_el _must_check_ _nonnull_all_ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion **array); _must_check_ _nonnull_all_ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **array); _must_check_ _nonnull_all_ int kmod_elf_get_dependency_symbols(const struct kmod_elf *elf, struct kmod_modversion **array); -_must_check_ _nonnull_all_ const void *kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags); +_must_check_ _nonnull_all_ int kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags, const void **stripped); /* * Debug mock lib need to find section ".gnu.linkonce.this_module" in order to diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 3054b9b1..5fc40a40 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -662,10 +662,11 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha return err; } - stripped = kmod_elf_strip(elf, flags); - if (stripped == NULL) { - ERR(mod->ctx, "Failed to strip version information: %m\n"); - return -errno; + err = kmod_elf_strip(elf, flags, &stripped); + if (err) { + ERR(mod->ctx, "Failed to strip version information: %s\n", + strerror(-err)); + return err; } mem = stripped; } else {