Fix a range checking bug in ng_int32_parse which affected 64-bit

machines.  The code formerly read:

    long val;
    if (val < (long)-0x80000000 || ...)
            return EINVAL;

The constant 0x80000000 has type unsigned int.  The unary `-'
operator does not change the type (or the value, in this case).
Therefore the promotion to long is done by 0-extension, giving
0x0000000080000000 instead of the desired 0xffffffff80000000.  I
got rid of the `-' and changed the cast to (int32_t) to give proper
sign-extension on all architectures and to better reflect the fact
that we are range-checking a 32-bit value.

This commit also makes the analogous changes to ng_int{8,16}_parse
for consistency.

MFC after:	3 days
This commit is contained in:
jdp 2001-05-19 19:36:32 +00:00
parent aac9daff0f
commit a8fdb15db6

View File

@ -345,7 +345,7 @@ ng_int8_parse(const struct ng_parse_type *type,
char *eptr;
val = strtol(s + *off, &eptr, 0);
if (val < -0x80 || val > 0xff || eptr == s + *off)
if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
return (EINVAL);
*off = eptr - s;
val8 = (int8_t)val;
@ -438,7 +438,8 @@ ng_int16_parse(const struct ng_parse_type *type,
char *eptr;
val = strtol(s + *off, &eptr, 0);
if (val < -0x8000 || val > 0xffff || eptr == s + *off)
if (val < (int16_t)0x8000
|| val > (u_int16_t)0xffff || eptr == s + *off)
return (EINVAL);
*off = eptr - s;
val16 = (int16_t)val;
@ -531,8 +532,8 @@ ng_int32_parse(const struct ng_parse_type *type,
char *eptr;
val = strtol(s + *off, &eptr, 0);
if (val < (long)-0x80000000
|| val > (u_long)0xffffffff || eptr == s + *off)
if (val < (int32_t)0x80000000
|| val > (u_int32_t)0xffffffff || eptr == s + *off)
return (EINVAL);
*off = eptr - s;
val32 = (int32_t)val;