From: Emil Velikov Date: Wed, 7 May 2025 12:04:25 +0000 (+0100) Subject: Use %m over strerror(errno) where possible X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fb0e59a12c19a683148cca343e4caec8b2667592;p=thirdparty%2Fkmod.git Use %m over strerror(errno) where possible The manual page of strerror() outlines a number of caveats wrt its usage. Swap for the GNU specific %m printf modifier (also supported on musl and bionic), which side-steps the issues as much as possible. Co-authored-by: Cristian Rodríguez [emil: split from larger patch, convert more instances to %m] 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-builtin.c b/libkmod/libkmod-builtin.c index 2012239a..da5abdc0 100644 --- a/libkmod/libkmod-builtin.c +++ b/libkmod/libkmod-builtin.c @@ -90,7 +90,7 @@ static ssize_t get_strings(struct kmod_builtin_info *info, const char *modname, if (n == -1) { if (!feof(info->fp)) { count = -errno; - ERR(info->ctx, "get_string: %s\n", strerror(errno)); + ERR(info->ctx, "get_string: %m\n"); } break; } @@ -182,7 +182,7 @@ ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname, *modinfo = strbuf_to_vector(&buf, (size_t)count); if (*modinfo == NULL) { count = -errno; - ERR(ctx, "strbuf_to_vector: %s\n", strerror(errno)); + ERR(ctx, "strbuf_to_vector: %m\n"); } } diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index a9d103e6..3054b9b1 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -334,7 +334,7 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, const char *path err = stat(abspath, &st); if (err < 0) { err = -errno; - DBG(ctx, "stat %s: %s\n", path, strerror(errno)); + DBG(ctx, "stat %s: %m\n", path); free(abspath); return err; } @@ -664,8 +664,7 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha stripped = kmod_elf_strip(elf, flags); if (stripped == NULL) { - ERR(mod->ctx, "Failed to strip version information: %s\n", - strerror(errno)); + ERR(mod->ctx, "Failed to strip version information: %m\n"); return -errno; } mem = stripped; @@ -1362,7 +1361,7 @@ KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, struct kmod_li fp = fopen("/proc/modules", "re"); if (fp == NULL) { int err = -errno; - ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno)); + ERR(ctx, "could not open /proc/modules: %m\n"); return err; } @@ -1434,7 +1433,7 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) { err = -errno; - DBG(mod->ctx, "could not open '%s': %s\n", path, strerror(-err)); + DBG(mod->ctx, "could not open '%s': %m\n", path); if (pathlen > (int)sizeof("/initstate") - 1) { struct stat st; @@ -1443,7 +1442,7 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) return KMOD_MODULE_COMING; err = -errno; - DBG(mod->ctx, "could not open '%s': %s\n", path, strerror(-err)); + DBG(mod->ctx, "could not open '%s': %m\n", path); } return err; } @@ -1499,7 +1498,7 @@ KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod) fp = fopen("/proc/modules", "re"); if (fp == NULL) { int err = -errno; - ERR(mod->ctx, "could not open /proc/modules: %s\n", strerror(errno)); + ERR(mod->ctx, "could not open /proc/modules: %m\n"); close(dfd); return err; } @@ -1551,7 +1550,7 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) fd = open(path, O_RDONLY | O_CLOEXEC); if (fd < 0) { err = -errno; - DBG(mod->ctx, "could not open '%s': %s\n", path, strerror(errno)); + DBG(mod->ctx, "could not open '%s': %m\n", path); return err; } @@ -1580,7 +1579,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module * d = opendir(dname); if (d == NULL) { - ERR(mod->ctx, "could not open '%s': %s\n", dname, strerror(errno)); + ERR(mod->ctx, "could not open '%s': %m\n", dname); return NULL; } @@ -1646,7 +1645,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module d = opendir(dname); if (d == NULL) { - ERR(mod->ctx, "could not open '%s': %s\n", dname, strerror(errno)); + ERR(mod->ctx, "could not open '%s': %m\n", dname); return NULL; } diff --git a/testsuite/delete_module.c b/testsuite/delete_module.c index 64b5b1cd..68a79220 100644 --- a/testsuite/delete_module.c +++ b/testsuite/delete_module.c @@ -137,8 +137,7 @@ static int remove_directory(const char *path) dir = opendir(path); if (!dir) { - ERR("Failed to open directory %s: %s (errno: %d)\n", path, - strerror(errno), errno); + ERR("Failed to open directory %s: %m (errno: %d)\n", path, errno); return -1; } diff --git a/tools/depmod.c b/tools/depmod.c index cd29bf4f..5f020f83 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -3001,7 +3001,7 @@ static int do_depmod(int argc, char *argv[]) optind++; } else { if (uname(&un) < 0) { - CRIT("uname() failed: %s\n", strerror(errno)); + CRIT("uname() failed: %m\n"); goto cmdline_failed; } cfg.kversion = un.release;