]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-strnum - do not invite undetectable errors
authorPhil Carmody <phil@dovecot.fi>
Tue, 8 Sep 2015 16:28:31 +0000 (19:28 +0300)
committerPhil Carmody <phil@dovecot.fi>
Tue, 8 Sep 2015 16:28:31 +0000 (19:28 +0300)
The very numbers which could cause a broken parser to over-run, the ones we
are testing, are the ones which will be parsed as having a value similar to
'value', so check that no value was returned by using a number completely
dissimilar to that. Otherwise, there might be an accidental mis-parse that
overwrote value, but left its value the same.

Signed-off-by: Phil Carmody <phil@dovecot.fi>
src/lib/test-strnum.c

index c4a9e69c12eadd7598840c93e006cadba3038e4e..2437c6591fb11065ea62af3306e93227edc15c7b 100644 (file)
@@ -28,7 +28,7 @@ static void test_str_to_uintmax(void)
 {
        unsigned int i=0;
        int randrange = rand()%15+1; /* when 1, will max out on 1s */
-       uintmax_t value = 0;
+       uintmax_t value = 0, valbase = rand() * 1000ull;
        int len, ret;
        char buff[50]; /* totally assumes < 159 bits */
 
@@ -73,9 +73,9 @@ static void test_str_to_uintmax(void)
                while (buff[--j] == '9')
                        buff[j] = '0';
                buff[j]++;
-
+               value = valbase + i;
                ret = str_to_uintmax(buff, &value);
-               test_assert_idx(ret < 0 && value == UINTMAX_MAX/9-1, i);
+               test_assert_idx(ret < 0 && value == valbase + i, i);
        }
        test_end();
 }
@@ -101,7 +101,7 @@ static void test_str_to_uintmax_hex(void)
 {
        unsigned int i=0;
        int randrange = rand()%15+1; /* when 1, will max out on 1s */
-       uintmax_t value = 0;
+       uintmax_t value = 0, valbase = rand() * 1000ull;
        int len, ret;
        char buff[52]; /* totally assumes < 200 bits */
 
@@ -149,8 +149,9 @@ static void test_str_to_uintmax_hex(void)
                        buff[j] = 'a';
                else
                        buff[j]++;
+               value = valbase + i;
                ret = str_to_uintmax_hex(buff, &value);
-               test_assert_idx(ret < 0 && value == UINTMAX_MAX/0x0f-1, i);
+               test_assert_idx(ret < 0 && value == valbase + i, i);
        }
        test_end();
 }
@@ -176,7 +177,7 @@ static void test_str_to_uintmax_oct(void)
 {
        unsigned int i=0;
        int randrange = rand()%15+1; /* when 1, will max out on 1s */
-       uintmax_t value = 0;
+       uintmax_t value = 0, valbase = rand() * 1000ull;
        int len, ret;
        char buff[69]; /* totally assumes < 200 bits */
 
@@ -221,8 +222,9 @@ static void test_str_to_uintmax_oct(void)
                while (buff[--j] == '7')
                        buff[j] = '0';
                buff[j]++;
+               value = valbase + i;
                ret = str_to_uintmax_oct(buff, &value);
-               test_assert_idx(ret < 0 && value == UINTMAX_MAX/007-1, i);
+               test_assert_idx(ret < 0 && value == valbase + i, i);
        }
        test_end();
 }