From: phk Date: Mon, 29 Jan 2007 22:06:33 +0000 (+0000) Subject: Things you didn't know about C, #7212: X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3813276936ff79e39e52c63a4d71e0c2b2461958;p=varnish Things you didn't know about C, #7212: There is no sane way to get sscanf to tell you how many characters were consumed, if you want to allow a variable number of arguments. The special format %n is patently useless for this, because you have to insert it at every conceiveable point in the string and that presumes full explicit whitespace markup. Parse -w argument "by hand", to catch illegal input like "1,INF,15" Tripped over by: Stein Ove Rosseland Fixes: ticket #82 git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1239 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 4e0e5c66..48c23ba1 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -32,6 +32,7 @@ */ #include +#include #include #include #include @@ -211,25 +212,47 @@ usage(void) static void tackle_warg(const char *argv) { - unsigned int ua, ub, uc; - - switch (sscanf(argv, "%u,%u,%u", &ua, &ub, &uc)) { - case 3: - params->wthread_timeout = uc; - /* FALLTHROUGH */ - case 2: - if (ub < ua) - usage(); - params->wthread_max = ub; - /* FALLTHROUGH */ - case 1: - if (ua < 1) - usage(); - params->wthread_min = ua; - break; - default: + unsigned int u; + char *ep, *eq; + + u = strtoul(argv, &ep, 0); + if (ep == argv) + usage(); + while (isspace(*ep)) + ep++; + if (u < 1) usage(); + params->wthread_min = u; + + if (*ep == '\0') { + params->wthread_max = params->wthread_min; + return; } + + if (*ep != ',') + usage(); + u = strtoul(++ep, &eq, 0); + if (eq == ep) + usage(); + if (u < params->wthread_min) + usage(); + while (isspace(*eq)) + eq++; + params->wthread_max = u; + + if (*eq == '\0') + return; + + if (*eq != ',') + usage(); + u = strtoul(++eq, &ep, 0); + if (ep == eq) + usage(); + while (isspace(*ep)) + ep++; + if (*ep != '\0') + usage(); + params->wthread_timeout = u; } /*--------------------------------------------------------------------