diff --git a/tools/depmod.c b/tools/depmod.c index 963fef63..e482648e 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -2073,6 +2074,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; @@ -2082,12 +2084,13 @@ static int output_deps_bin(struct depmod *depmod, FILE *out) return -ENOMEM; array_init(&array, 8); + 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)) { @@ -2095,45 +2098,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 = sbuf.used > 0 ? 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);