]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: return the errno from kmod_elf_strip()
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 7 May 2025 17:58:11 +0000 (18:58 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 20 May 2025 02:51:44 +0000 (21:51 -0500)
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 <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/346
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-elf.c
libkmod/libkmod-internal.h
libkmod/libkmod-module.c

index f215f1ee0020f89751ecb42ce245d1731c44c603..4590543cd5f3c6adb67ed788f47816bf796a6413 100644 (file)
@@ -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,
index 4706071d71adb0ea7a4a115249c5aa5c5355e8a5..8d322b65b4f60c15a4ddbaa579c92a86cdec0e9c 100644 (file)
@@ -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
index 3054b9b16df593d5d188835e6f19dcefa0e761ab..5fc40a408b65cff123c19b004393941657ca73bb 100644 (file)
@@ -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 {