From 358d2d6534e6179eb24d2c73ba2c46e2f27ef486 Mon Sep 17 00:00:00 2001 From: des Date: Thu, 29 Mar 2007 10:49:58 +0000 Subject: [PATCH] The argv length calculation was not only off by one, but failed to take into account the extra space required by expanded quotes, backslashes and newlines. Instead of pre-allocating a (possibly too short) buffer, start with a 64-byte buffer and double it every time we come close to filling it up. Also, avoid appending a trailing space before the final newline. This issue was uncovered by Kristoffer Gleditsch , who also helped test this patch. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1287 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/mgt_cli.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/varnish-cache/bin/varnishd/mgt_cli.c b/varnish-cache/bin/varnishd/mgt_cli.c index 08adbb67..032bd3d8 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.c +++ b/varnish-cache/bin/varnishd/mgt_cli.c @@ -95,14 +95,23 @@ mcf_passthru(struct cli *cli, char **av, void *priv) cli_out(cli, "Cache process not running"); return; } - v = 0; - for (u = 1; av[u] != NULL; u++) - v += strlen(av[u]) + 3; + v = 64; p = malloc(v); XXXAN(p); q = p; for (u = 1; av[u] != NULL; u++) { + if (v < (q - p) + 8) { + r = realloc(p, v + v); + XXXAN(r); + v += v; + q += r - p; + p = r; + } + /* v >= (q - p) + 8 */ + if (u > 1) + *q++ = ' '; *q++ = '"'; + /* v >= (q - p) + 6 */ for (r = av[u]; *r; r++) { switch (*r) { case '\\': *q++ = '\\'; *q++ = '\\'; break; @@ -111,9 +120,10 @@ mcf_passthru(struct cli *cli, char **av, void *priv) default: *q++ = *r; break; } } + /* v >= (q - p) + 4 */ *q++ = '"'; - *q++ = ' '; } + /* v >= (q - p) + 3 */ *q++ = '\n'; v = q - p; i = write(cli_o, p, v); -- 2.39.5