From: Tobias Stoeckmann Date: Fri, 18 Oct 2024 15:28:36 +0000 (+0200) Subject: depmod: Use strbuf for dependency output X-Git-Tag: v34~200 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=10c8bb097622f434274bdef4aeb54a77d3176aab;p=thirdparty%2Fkmod.git depmod: Use strbuf for dependency output 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 Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/193 Signed-off-by: Lucas De Marchi --- diff --git a/tools/depmod.c b/tools/depmod.c index cbac467c..3328103e 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -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);