Mailing List Archive

r1239 - trunk/varnish-cache/bin/varnishd
Author: phk
Date: 2007-01-29 23:06:33 +0100 (Mon, 29 Jan 2007)
New Revision: 1239

Modified:
trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
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 <steinove at vg.no>

Fixes: ticket #82



Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c 2007-01-22 13:24:42 UTC (rev 1238)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c 2007-01-29 22:06:33 UTC (rev 1239)
@@ -32,6 +32,7 @@
*/

#include <err.h>
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -211,25 +212,47 @@
static void
tackle_warg(const char *argv)
{
- unsigned int ua, ub, uc;
+ unsigned int u;
+ char *ep, *eq;

- 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:
+ 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;
}

/*--------------------------------------------------------------------