Try to make $if_match better handle stranger inputs
authorPavel Labath <pavelo@centrum.sk>
Wed, 17 Feb 2010 18:43:16 +0000 (19:43 +0100)
committerPavel Labath <pavelo@centrum.sk>
Wed, 17 Feb 2010 18:45:22 +0000 (19:45 +0100)
This should fix sf.net #2953283

src/algebra.c
src/algebra.h

index 3f41e33..a1bcda5 100644 (file)
@@ -126,11 +126,9 @@ enum arg_type get_arg_type(const char *arg)
        const char *p, *e;
 
        p = arg;
-       e = arg + strlen(arg);
+       e = arg + strlen(arg)-1;
 
-       if (*(e - 1) == ' ')
-               e--;
-       while (*e && *e == ' ')
+       while (p != e && *e && *e == ' ')
                e--;
        while (p != e && *p == ' ')
                p++;
@@ -140,23 +138,23 @@ enum arg_type get_arg_type(const char *arg)
 
        if (*p == '-')  //allow negative values
                p++;
-       while (p != e) {
+       while (p <= e) {
                if (!isdigit(*p))
                        break;
                p++;
        }
-       if (p == e)
+       if (p == e+1)
                return ARG_LONG;
        if (*p == '.') {
                p++;
-               while (p != e) {
+               while (p <= e) {
                        if (!isdigit(*p))
-                               return ARG_STRING;
+                               return ARG_BAD;
                        p++;
                }
                return ARG_DOUBLE;
        }
-       return ARG_STRING;
+       return ARG_BAD;
 }
 
 char *arg_to_string(const char *arg)
@@ -213,6 +211,10 @@ int compare(const char *expr)
 
        type1 = get_arg_type(expr_dup);
        type2 = get_arg_type(expr_dup + idx + 1);
+       if (type1 == ARG_BAD || type2 == ARG_BAD) {
+               NORM_ERR("Bad arguments: '%s' and '%s'", expr_dup, (expr_dup + idx + 1));
+               return -2;
+       }
        if (type1 == ARG_LONG && type2 == ARG_DOUBLE)
                type1 = ARG_DOUBLE;
        if (type1 == ARG_DOUBLE && type2 == ARG_LONG)
@@ -239,6 +241,7 @@ int compare(const char *expr)
                case ARG_DOUBLE:
                        return dcompare(arg_to_double(expr_dup), mtype,
                                        arg_to_double(expr_dup + idx + 1));
+               case ARG_BAD: /* make_gcc_happy() */;
        }
        /* not reached */
        return -2;
index e338e5f..4e4b721 100644 (file)
@@ -40,6 +40,7 @@ enum match_type {
 };
 
 enum arg_type {
+       ARG_BAD = 0,    /* something strange */
        ARG_STRING = 1, /* "asdf" */
        ARG_LONG = 2,   /* 123456 */
        ARG_DOUBLE = 3, /* 12.456 */