Skip to content

Reduce strerror and errno usage #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions libkmod/libkmod-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,27 @@
char *buf;
};

static bool kmod_builtin_info_init(struct kmod_builtin_info *info, struct kmod_ctx *ctx)
static int kmod_builtin_info_init(struct kmod_builtin_info *info, struct kmod_ctx *ctx)
{
char path[PATH_MAX];
FILE *fp = NULL;
const char *dirname = kmod_get_dirname(ctx);
size_t len = strlen(dirname);

if ((len + 1 + strlen(MODULES_BUILTIN_MODINFO) + 1) >= sizeof(path)) {
errno = ENAMETOOLONG;
return false;
}
if ((len + 1 + strlen(MODULES_BUILTIN_MODINFO) + 1) >= sizeof(path))
return -ENAMETOOLONG;
snprintf(path, sizeof(path), "%s/" MODULES_BUILTIN_MODINFO, dirname);

fp = fopen(path, "r");
if (fp == NULL)
return false;
return -errno;

Check warning on line 49 in libkmod/libkmod-builtin.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-builtin.c#L49

Added line #L49 was not covered by tests

info->ctx = ctx;
info->fp = fp;
info->bufsz = 0;
info->buf = NULL;

return true;
return 0;
}

static void kmod_builtin_info_release(struct kmod_builtin_info *info)
Expand All @@ -64,19 +62,6 @@
fclose(info->fp);
}

static ssize_t get_string(struct kmod_builtin_info *info)
{
ssize_t len;

len = getdelim(&info->buf, &info->bufsz, '\0', info->fp);
if (len > 0 && info->buf[len] != '\0') {
errno = EINVAL;
len = -1;
}

return len;
}

static ssize_t get_strings(struct kmod_builtin_info *info, const char *modname,
struct strbuf *buf)
{
Expand All @@ -86,11 +71,11 @@
for (count = 0; count < INTPTR_MAX;) {
char *dot, *line;

n = get_string(info);
n = getdelim(&info->buf, &info->bufsz, '\0', info->fp);
if (n == -1) {
if (!feof(info->fp)) {
count = -errno;
ERR(info->ctx, "get_string: %s\n", strerror(errno));
ERR(info->ctx, "get_strings: %m\n");
}
break;
}
Expand Down Expand Up @@ -142,10 +127,8 @@
/* (string vector + NULL) * sizeof(char *) + strbuf_used() */
if (uaddsz_overflow(count, 1, &n) ||
umulsz_overflow(sizeof(char *), n, &vec_size) ||
uaddsz_overflow(strbuf_used(buf), vec_size, &total_size)) {
errno = ENOMEM;
uaddsz_overflow(strbuf_used(buf), vec_size, &total_size))
return NULL;
}

vector = realloc(buf->bytes, total_size);
if (vector == NULL)
Expand All @@ -171,18 +154,20 @@
DECLARE_STRBUF(buf);
struct kmod_builtin_info info;
ssize_t count;
int ret;

if (!kmod_builtin_info_init(&info, ctx))
return -errno;
ret = kmod_builtin_info_init(&info, ctx);
if (ret < 0)
return ret;

Check warning on line 161 in libkmod/libkmod-builtin.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-builtin.c#L161

Added line #L161 was not covered by tests

count = get_strings(&info, modname, &buf);
if (count == 0)
*modinfo = NULL;
else if (count > 0) {
*modinfo = strbuf_to_vector(&buf, (size_t)count);
if (*modinfo == NULL) {
count = -errno;
ERR(ctx, "strbuf_to_vector: %s\n", strerror(errno));
count = -ENOMEM;

Check warning on line 169 in libkmod/libkmod-builtin.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-builtin.c#L169

Added line #L169 was not covered by tests
ERR(ctx, "strbuf_to_vector: %m\n");
}
}

Expand Down
38 changes: 14 additions & 24 deletions libkmod/libkmod-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
}
}

struct kmod_elf *kmod_elf_new(const void *memory, off_t size)
int kmod_elf_new(const void *memory, off_t size, struct kmod_elf **out_elf)
{
struct kmod_elf *elf;
size_t shdrs_size, shdr_size;
Expand All @@ -317,21 +317,14 @@
assert_cc(sizeof(uint32_t) == sizeof(Elf32_Word));
assert_cc(sizeof(uint32_t) == sizeof(Elf64_Word));

if (!memory) {
errno = -EINVAL;
return NULL;
}

elf = malloc(sizeof(struct kmod_elf));
if (elf == NULL) {
return NULL;
}
if (elf == NULL)
return -ENOMEM;

err = elf_identify(elf, memory, size);
if (err < 0) {
free(elf);
errno = -err;
return NULL;
return err;

Check warning on line 327 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L327

Added line #L327 was not covered by tests
}

elf->memory = memory;
Expand Down Expand Up @@ -393,12 +386,12 @@
}

kmod_elf_save_sections(elf);
return elf;
*out_elf = elf;
return 0;

invalid:
free(elf);
errno = EINVAL;
return NULL;
return -EINVAL;

Check warning on line 394 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L394

Added line #L394 was not covered by tests
}

void kmod_elf_unref(struct kmod_elf *elf)
Expand Down Expand Up @@ -666,7 +659,7 @@
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;
Expand All @@ -675,30 +668,27 @@

changed = memdup(elf->memory, elf->size);
if (changed == NULL)
return NULL;
return -errno;

Check warning on line 671 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L671

Added line #L671 was not covered by tests

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;

Check warning on line 691 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L691

Added line #L691 was not covered by tests
}

static int kmod_elf_get_symbols_symtab(const struct kmod_elf *elf,
Expand Down
1 change: 0 additions & 1 deletion libkmod/libkmod-file-zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ int kmod_file_load_zlib(struct kmod_file *file)
return -EINVAL;
}

errno = 0;
gzfd = fcntl(file->fd, F_DUPFD_CLOEXEC, 3);
if (gzfd < 0)
return -errno;
Expand Down
54 changes: 28 additions & 26 deletions libkmod/libkmod-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,53 +58,53 @@
// clang-format on
};

struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
int kmod_file_get_elf(struct kmod_file *file, struct kmod_elf **elf)
{
int err;

if (file->elf)
return file->elf;

err = kmod_file_load_contents(file);
if (err) {
errno = -err;
return NULL;
if (!file->elf) {
int err = kmod_file_load_contents(file);
if (err)
return err;

err = kmod_elf_new(file->memory, file->size, &file->elf);
if (err)
return err;
}

file->elf = kmod_elf_new(file->memory, file->size);
return file->elf;
*elf = file->elf;
return 0;
}

struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename)
int kmod_file_open(const struct kmod_ctx *ctx, const char *filename,
struct kmod_file **out_file)
{
struct kmod_file *file;
_cleanup_free_ struct kmod_file *file;
char buf[7];
ssize_t sz;
int ret;

assert_cc(sizeof(magic_zstd) < sizeof(buf));
assert_cc(sizeof(magic_xz) < sizeof(buf));
assert_cc(sizeof(magic_zlib) < sizeof(buf));

file = calloc(1, sizeof(struct kmod_file));
if (file == NULL)
return NULL;
if (file == NULL) {
file = NULL;
return -ENOMEM;
}

file->fd = open(filename, O_RDONLY | O_CLOEXEC);
if (file->fd < 0) {
free(file);
return NULL;
}
if (file->fd < 0)
return -errno;

Check warning on line 97 in libkmod/libkmod-file.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-file.c#L97

Added line #L97 was not covered by tests

sz = pread_str_safe(file->fd, buf, sizeof(buf), 0);
if (sz != (sizeof(buf) - 1)) {
if (sz < 0)
errno = -sz;
ret = (int)sz;

Check warning on line 102 in libkmod/libkmod-file.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-file.c#L102

Added line #L102 was not covered by tests
else
errno = EINVAL;
ret = -EINVAL;

close(file->fd);
free(file);
return NULL;
return ret;

Check warning on line 107 in libkmod/libkmod-file.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-file.c#L107

Added line #L107 was not covered by tests
}

for (unsigned int i = 0; i < ARRAY_SIZE(comp_types); i++) {
Expand All @@ -120,7 +120,9 @@

file->ctx = ctx;

return file;
*out_file = file;
file = NULL;
return 0;
}

/*
Expand All @@ -135,7 +137,7 @@
return file->load(file);
}

void *kmod_file_get_contents(const struct kmod_file *file)
const void *kmod_file_get_contents(const struct kmod_file *file)
{
return file->memory;
}
Expand Down
13 changes: 7 additions & 6 deletions libkmod/libkmod-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,32 @@ _nonnull_(1) void kmod_module_set_required(struct kmod_module *mod, bool require
_nonnull_all_ bool kmod_module_is_builtin(struct kmod_module *mod);

/* libkmod-file.c */
_must_check_ _nonnull_all_ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename);
_nonnull_all_ struct kmod_elf *kmod_file_get_elf(struct kmod_file *file);
struct kmod_file;
struct kmod_elf;
_must_check_ _nonnull_all_ int kmod_file_open(const struct kmod_ctx *ctx, const char *filename, struct kmod_file **file);
_must_check_ _nonnull_all_ int kmod_file_get_elf(struct kmod_file *file, struct kmod_elf **elf);
_nonnull_all_ int kmod_file_load_contents(struct kmod_file *file);
_must_check_ _nonnull_all_ void *kmod_file_get_contents(const struct kmod_file *file);
_must_check_ _nonnull_all_ const void *kmod_file_get_contents(const struct kmod_file *file);
_must_check_ _nonnull_all_ off_t kmod_file_get_size(const struct kmod_file *file);
_must_check_ _nonnull_all_ enum kmod_file_compression_type kmod_file_get_compression(const struct kmod_file *file);
_must_check_ _nonnull_all_ int kmod_file_get_fd(const struct kmod_file *file);
_nonnull_all_ void kmod_file_unref(struct kmod_file *file);

/* libkmod-elf.c */
struct kmod_elf;
struct kmod_modversion {
uint64_t crc;
enum kmod_symbol_bind bind;
const char *symbol;
};

struct kmod_elf *kmod_elf_new(const void *memory, off_t size);
_must_check_ _nonnull_all_ int kmod_elf_new(const void *memory, off_t size, struct kmod_elf **elf);
_nonnull_all_ void kmod_elf_unref(struct kmod_elf *elf);
_must_check_ _nonnull_all_ const void *kmod_elf_get_memory(const struct kmod_elf *elf);
_must_check_ _nonnull_all_ int kmod_elf_get_modinfo_strings(const struct kmod_elf *elf, char ***array);
_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
Expand Down
Loading
Loading