]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
depmod: Use strbuf for dependency output
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 18 Oct 2024 15:28:36 +0000 (17:28 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 22 Oct 2024 16:52:48 +0000 (11:52 -0500)
Use shared/strbuf instead of manually re-implementing its features.
Reduces the amount of custom code in depmod, simplifies auditing,
reduces binary size, and has the nice benefit of slightly faster
runtime due to memory reusage.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/193
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
tools/depmod.c

index cbac467cd27c9e3a8c6f7a64f224710603bc7d85..3328103e24c72cbc96e168ef3c1e3f1e629cf43c 100644 (file)
@@ -23,6 +23,7 @@
 #include <shared/array.h>
 #include <shared/hash.h>
 #include <shared/macro.h>
+#include <shared/strbuf.h>
 #include <shared/util.h>
 #include <shared/scratchbuf.h>
 
@@ -2076,6 +2077,7 @@ static int output_deps_bin(struct depmod *depmod, FILE *out)
        struct index_node *idx;
        size_t i;
        struct array array;
+       struct strbuf sbuf;
 
        if (out == stdout)
                return 0;
@@ -2085,12 +2087,13 @@ static int output_deps_bin(struct depmod *depmod, FILE *out)
                return -ENOMEM;
 
        array_init(&array, 64);
+       strbuf_init(&sbuf);
 
        for (i = 0; i < depmod->modules.count; i++) {
                const struct mod *mod = depmod->modules.array[i];
                const char *p = mod_get_compressed_path(mod);
-               char *line;
-               size_t j, linepos, linelen, slen;
+               const char *line;
+               size_t j;
                int duplicate;
 
                if (!mod_get_all_sorted_dependencies(mod, &array)) {
@@ -2098,45 +2101,29 @@ static int output_deps_bin(struct depmod *depmod, FILE *out)
                        continue;
                }
 
-               linelen = strlen(p) + 1;
-               for (j = 0; j < array.count; j++) {
-                       const struct mod *d = array.array[j];
-                       linelen += 1 + strlen(mod_get_compressed_path(d));
-               }
-
-               line = malloc(linelen + 1);
-               if (line == NULL) {
-                       ERR("modules.deps.bin: out of memory\n");
+               strbuf_clear(&sbuf);
+               if (!strbuf_pushchars(&sbuf, p) || !strbuf_pushchar(&sbuf, ':')) {
+                       ERR("could not write dependencies of %s\n", p);
                        continue;
                }
 
-               linepos = 0;
-               slen = strlen(p);
-               memcpy(line + linepos, p, slen);
-               linepos += slen;
-               line[linepos] = ':';
-               linepos++;
-
                for (j = 0; j < array.count; j++) {
                        const struct mod *d = array.array[j];
-                       const char *dp;
-
-                       line[linepos] = ' ';
-                       linepos++;
+                       const char *dp = mod_get_compressed_path(d);
 
-                       dp = mod_get_compressed_path(d);
-                       slen = strlen(dp);
-                       memcpy(line + linepos, dp, slen);
-                       linepos += slen;
+                       if (!strbuf_pushchar(&sbuf, ' ') || !strbuf_pushchars(&sbuf, dp)) {
+                               ERR("could not write dependencies of %s\n", p);
+                               continue;
+                       }
                }
-               line[linepos] = '\0';
+               line = strbuf_str(&sbuf);
 
                duplicate = index_insert(idx, mod->modname, line, mod->idx);
                if (duplicate && depmod->cfg->warn_dups)
                        WRN("duplicate module deps:\n%s\n", line);
-               free(line);
        }
 
+       strbuf_release(&sbuf);
        array_free_array(&array);
        index_write(idx, out);
        index_destroy(idx);