]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: use strlen() for string literals
authorEmil Velikov <emil.l.velikov@gmail.com>
Sat, 24 May 2025 11:06:13 +0000 (12:06 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 29 May 2025 22:10:16 +0000 (17:10 -0500)
Older compilers had struggles expanding strlen(string-literal) to a
constant compile time expression. Thus our code-base used sizeof() - 1
instead.

This has been resolved for years, so let's use the correct function. As
a bonus point, we can remove a few variable making the code tad easier
to follow.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/354
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-config.c
libkmod/libkmod-elf.c
libkmod/libkmod-module.c

index 9e03f3c594724ad1a1d49771047cd5e11f8a5ce0..38b162d5f284b4832542f1f195fed948dee0fb4a 100644 (file)
@@ -267,11 +267,10 @@ static int kmod_config_add_softdep(struct kmod_config *config, const char *modna
                }
                plen = s - p;
 
-               if (plen == sizeof("pre:") - 1 &&
-                   memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
+               if (plen == strlen("pre:") && memcmp(p, "pre:", strlen("pre:")) == 0)
                        mode = S_PRE;
-               else if (plen == sizeof("post:") - 1 &&
-                        memcmp(p, "post:", sizeof("post:") - 1) == 0)
+               else if (plen == strlen("post:") &&
+                        memcmp(p, "post:", strlen("post:")) == 0)
                        mode = S_POST;
                else if (*s != '\0' || (*s == '\0' && !was_space)) {
                        if (mode == S_PRE) {
@@ -352,11 +351,10 @@ static int kmod_config_add_softdep(struct kmod_config *config, const char *modna
                }
                plen = s - p;
 
-               if (plen == sizeof("pre:") - 1 &&
-                   memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
+               if (plen == strlen("pre:") && memcmp(p, "pre:", strlen("pre:")) == 0)
                        mode = S_PRE;
-               else if (plen == sizeof("post:") - 1 &&
-                        memcmp(p, "post:", sizeof("post:") - 1) == 0)
+               else if (plen == strlen("post:") &&
+                        memcmp(p, "post:", strlen("post:")) == 0)
                        mode = S_POST;
                else if (*s != '\0' || (*s == '\0' && !was_space)) {
                        if (mode == S_PRE) {
@@ -506,8 +504,6 @@ static int kmod_config_add_weakdep(struct kmod_config *config, const char *modna
 
 static char *softdep_to_char(struct kmod_softdep *dep)
 {
-       const size_t sz_preprefix = sizeof("pre: ") - 1;
-       const size_t sz_postprefix = sizeof("post: ") - 1;
        size_t sz = 1; /* at least '\0' */
        size_t sz_pre, sz_post;
        const char *start, *end;
@@ -521,7 +517,7 @@ static char *softdep_to_char(struct kmod_softdep *dep)
                start = dep->pre[0];
                end = dep->pre[dep->n_pre - 1] + strlen(dep->pre[dep->n_pre - 1]);
                sz_pre = end - start;
-               sz += sz_pre + sz_preprefix;
+               sz += sz_pre + strlen("pre: ");
        } else
                sz_pre = 0;
 
@@ -529,7 +525,7 @@ static char *softdep_to_char(struct kmod_softdep *dep)
                start = dep->post[0];
                end = dep->post[dep->n_post - 1] + strlen(dep->post[dep->n_post - 1]);
                sz_post = end - start;
-               sz += sz_post + sz_postprefix;
+               sz += sz_post + strlen("post: ");
        } else
                sz_post = 0;
 
@@ -540,8 +536,8 @@ static char *softdep_to_char(struct kmod_softdep *dep)
        if (sz_pre) {
                char *p;
 
-               memcpy(itr, "pre: ", sz_preprefix);
-               itr += sz_preprefix;
+               memcpy(itr, "pre: ", strlen("pre: "));
+               itr += strlen("pre: ");
 
                /* include last '\0' */
                memcpy(itr, dep->pre[0], sz_pre + 1);
@@ -555,8 +551,8 @@ static char *softdep_to_char(struct kmod_softdep *dep)
        if (sz_post) {
                char *p;
 
-               memcpy(itr, "post: ", sz_postprefix);
-               itr += sz_postprefix;
+               memcpy(itr, "post: ", strlen("post: "));
+               itr += strlen("post: ");
 
                /* include last '\0' */
                memcpy(itr, dep->post[0], sz_post + 1);
index 4775360d977e430b34ed36b1ae6666d0e764555c..23d27f027340bc8c2e7651ce831adf0bf1e7e471 100644 (file)
@@ -640,8 +640,7 @@ static int elf_strip_vermagic(const struct kmod_elf *elf, uint8_t *changed)
                        continue;
 
                s = strings + i;
-               len = sizeof("vermagic=") - 1;
-               if (i + len >= size)
+               if (i + strlen("vermagic=") >= size)
                        continue;
                if (!strstartswith(s, "vermagic=")) {
                        i += strlen(s);
index 796dafa1837292dd4328f351a5bc80a4295439e3..3ca90c0ce9d82a7834f46e92aef75614e95f7a2e 100644 (file)
@@ -819,7 +819,7 @@ static int module_do_install_commands(struct kmod_module *mod, const char *optio
 
        options_len = strlen(options);
        cmdlen = strlen(command);
-       varlen = sizeof("$CMDLINE_OPTS") - 1;
+       varlen = strlen("$CMDLINE_OPTS");
 
        cmd = memdup(command, cmdlen + 1);
        if (cmd == NULL)
@@ -1432,9 +1432,9 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
                err = -errno;
                DBG(mod->ctx, "could not open '%s': %m\n", path);
 
-               if (pathlen > (int)sizeof("/initstate") - 1) {
+               if (pathlen > (int)strlen("/initstate")) {
                        struct stat st;
-                       path[pathlen - (sizeof("/initstate") - 1)] = '\0';
+                       path[pathlen - strlen("/initstate")] = '\0';
                        if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
                                return KMOD_MODULE_COMING;