]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: strnum - simplify hex and oct overflow code
authorPhil Carmody <phil@dovecot.fi>
Thu, 3 Sep 2015 11:01:57 +0000 (14:01 +0300)
committerPhil Carmody <phil@dovecot.fi>
Thu, 3 Sep 2015 11:01:57 +0000 (14:01 +0300)
uintmax_t is defined to have modulo-2^n semantics, and therefore the bottom
bits of (uintmax_t) are guaranteed to be all set. Therefore the checking of
the next character read is unnecessary, as it's already done in the loop
control statement itself. (This is not true about the bottom digit base 10,
which is why the check remains in the decimal case)

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

index 5ed2445d349013e61fd8dc9940452f6a862240e3..aa82159b082deda2a4cf73baa63ee046453efaf3 100644 (file)
@@ -160,12 +160,8 @@ int str_parse_uintmax_hex(const char *str, uintmax_t *num_r,
                return -1;
 
        do {
-               if (n >= ((uintmax_t)-1 >> 4)) {
-                       if (n > (uintmax_t)-1 >> 4)
-                               return -1;
-                       if ((uintmax_t)hex > ((uintmax_t)-1 & 0x0f))
-                               return -1;
-               }
+               if (n > (uintmax_t)-1 >> 4)
+                       return -1;
                n = (n << 4) + hex;
                str++;
        } while (_str_parse_hex(*str, &hex) >= 0);
@@ -230,12 +226,8 @@ int str_parse_uintmax_oct(const char *str, uintmax_t *num_r,
                return -1;
 
        for (; *str >= '0' && *str <= '7'; str++) {
-               if (n >= ((uintmax_t)-1 >> 3)) {
-                       if (n > (uintmax_t)-1 >> 3)
-                               return -1;
-                       if ((uintmax_t)(*str - '0') > ((uintmax_t)-1 & 0x03))
-                               return -1;
-               }
+               if (n > (uintmax_t)-1 >> 3)
+                       return -1;
                n = (n << 3) + (*str - '0');
        }
        if (endp_r != NULL)