Message ID | 20221215170216.hm6i5akmxbz6u62j@tarta.nabijaczleweli.xyz (mailing list archive) |
---|---|
State | Changes Requested |
Delegated to: | Herbert Xu |
Headers | show |
Series | [v2] parser: don't keep alloca()ing in a loop for substitutions | expand |
наб <nabijaczleweli@nabijaczleweli.xyz> wrote: > > Naturally, I hadn't considered that. > > This version I've run through valgrind in a good few configurations and > am happy to conclude there are no leaks (and the memory usage bump is > imperceptible unless you were almost-crashing anyway). Nice work! > @@ -1446,10 +1443,8 @@ done: > if (oldstyle) > tokpushback = 0; > out = growstackto(savelen + 1); > - if (str) { > - memcpy(out, str, savelen); > - STADJUST(savelen, out); > - } > + memcpy(out, str, savelen); > + STADJUST(savelen, out); Minor nit but these three lines can be combined into: out = stnputs(stackblock(), str, savelen); Thanks,
diff --git a/src/parser.c b/src/parser.c index a552c47..89698cb 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1360,12 +1360,9 @@ parsebackq: { struct heredoc *saveheredoclist; int uninitialized_var(saveprompt); - str = NULL; + str = stackblock(); savelen = out - (char *)stackblock(); - if (savelen > 0) { - str = alloca(savelen); - memcpy(str, stackblock(), savelen); - } + grabstackblock(savelen); if (oldstyle) { /* We must read until the closing backquote, giving special treatment to some slashes, and then push the string and @@ -1446,10 +1443,8 @@ done: if (oldstyle) tokpushback = 0; out = growstackto(savelen + 1); - if (str) { - memcpy(out, str, savelen); - STADJUST(savelen, out); - } + memcpy(out, str, savelen); + STADJUST(savelen, out); USTPUTC(CTLBACKQ, out); if (oldstyle) goto parsebackq_oldreturn;