Skip to content

Commit 132eb4c

Browse files
evelikovlucasdemarchi
authored andcommitted
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: #244 Signed-off-by: Emil Velikov <[email protected]> Link: #346 Signed-off-by: Lucas De Marchi <[email protected]>
1 parent c8b69b7 commit 132eb4c

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

libkmod/libkmod-elf.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ static int elf_strip_vermagic(const struct kmod_elf *elf, uint8_t *changed)
666666
return -ENODATA;
667667
}
668668

669-
const void *kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags)
669+
int kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags, const void **stripped)
670670
{
671671
uint8_t *changed;
672672
int err = 0;
@@ -675,30 +675,27 @@ const void *kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags)
675675

676676
changed = memdup(elf->memory, elf->size);
677677
if (changed == NULL)
678-
return NULL;
678+
return -errno;
679679

680680
ELFDBG(elf, "copied memory to allow writing.\n");
681681

682682
if (flags & KMOD_INSERT_FORCE_MODVERSION) {
683683
err = elf_strip_versions_section(elf, changed);
684-
if (err < 0) {
685-
errno = -err;
684+
if (err < 0)
686685
goto fail;
687-
}
688686
}
689687

690688
if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
691689
err = elf_strip_vermagic(elf, changed);
692-
if (err < 0) {
693-
errno = -err;
690+
if (err < 0)
694691
goto fail;
695-
}
696692
}
697693

698-
return changed;
694+
*stripped = changed;
695+
return 0;
699696
fail:
700697
free(changed);
701-
return NULL;
698+
return err;
702699
}
703700

704701
static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,

libkmod/libkmod-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ _must_check_ _nonnull_all_ int kmod_elf_get_modinfo_strings(const struct kmod_el
159159
_must_check_ _nonnull_all_ int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion **array);
160160
_must_check_ _nonnull_all_ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **array);
161161
_must_check_ _nonnull_all_ int kmod_elf_get_dependency_symbols(const struct kmod_elf *elf, struct kmod_modversion **array);
162-
_must_check_ _nonnull_all_ const void *kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags);
162+
_must_check_ _nonnull_all_ int kmod_elf_strip(const struct kmod_elf *elf, unsigned int flags, const void **stripped);
163163

164164
/*
165165
* Debug mock lib need to find section ".gnu.linkonce.this_module" in order to

libkmod/libkmod-module.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,11 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha
662662
return err;
663663
}
664664

665-
stripped = kmod_elf_strip(elf, flags);
666-
if (stripped == NULL) {
667-
ERR(mod->ctx, "Failed to strip version information: %m\n");
668-
return -errno;
665+
err = kmod_elf_strip(elf, flags, &stripped);
666+
if (err) {
667+
ERR(mod->ctx, "Failed to strip version information: %s\n",
668+
strerror(-err));
669+
return err;
669670
}
670671
mem = stripped;
671672
} else {

0 commit comments

Comments
 (0)