]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Use %m over strerror(errno) where possible
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 7 May 2025 12:04:25 +0000 (13:04 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 20 May 2025 02:51:43 +0000 (21:51 -0500)
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 <cristian@rodriguez.im>
[emil: split from larger patch, convert more instances to %m]
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-builtin.c
libkmod/libkmod-module.c
testsuite/delete_module.c
tools/depmod.c

index 2012239ad4af1826420ffeb0b5755a28356a5f2a..da5abdc0e2021a49f65ebc53c8f2aec20643ac3a 100644 (file)
@@ -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");
                }
        }
 
index a9d103e6dba2524d24f212c25e0d528c24f86b56..3054b9b16df593d5d188835e6f19dcefa0e761ab 100644 (file)
@@ -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;
        }
 
index 64b5b1cd85aef627530766bf06673333110b5896..68a7922048c5d47f058e594acaeccd378b87239d 100644 (file)
@@ -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;
        }
 
index cd29bf4fbe69fe18f60116b32322be21e9fea3b3..5f020f8309cb815978f94eb5c52209d897476e03 100644 (file)
@@ -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;