]> err.no Git - varnish/commitdiff
Things you didn't know about C, #7212:
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 29 Jan 2007 22:06:33 +0000 (22:06 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 29 Jan 2007 22:06:33 +0000 (22:06 +0000)
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@vg.no>

Fixes: ticket #82
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1239 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/varnishd.c

index 4e0e5c66a2134762ce82bb5029214780b08bc9d3..48c23ba1fe4958f2497ca977b7fcdd4d1c2865a7 100644 (file)
@@ -32,6 +32,7 @@
  */
 
 #include <err.h>
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -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;
 }
 
 /*--------------------------------------------------------------------