C
CBFalconer
Why? Consider the following:
/* assuming ULONG_MAX == 4294967295 */
unsigned long ul1 = -2UL; // perfectly good, ul1 = 4294967294
unsigned long ul2 = 4294967297UL; // error, overflow
The first is actually -(2UL), which has been specifically defined
to be interpreted with modulo arithmetic. However the string "-2"
represents a value outside the range of an unsigned long. The
purpose of error detection in strtoul() is to detect the supply of
an invalid value by the user.
I would suggest that the proper interpretation of that string is
to set endptr to point after the 2 (which may be to a '\0'),
return ULONG_MAX, and set errno to ERANGE.
To achieve the modulo interpretation, the programmer should have
to write:
unsigned long u;
u = -2;
or
u = strtol("-2", NULL, 10);
which last will have exactly the same values to use in the actual
assignment.