Mail Archives: djgpp/2004/02/23/12:15:24
DJ Delorie wrote:
>
> > BTW the following shows up a glitch in DJGPP 2.03 system. Calling
> > the program with an argument of -1 returns the overflow condition,
> > because strtoul returns ULONG_MAX rather than 0. Cross-posted to
> > comp.os.msdos.djgpp for this.
>
> strtoul is *supposed* to return ULONG_MAX if it detects a range error.
That isn't a range error IMO - the initial '-' is an illegal char,
so it shouldn't convert anything, and should return 0. From N869:
[#4] The subject sequence is defined as the longest initial
subsequence of the input string, starting with the first
non-white-space character, that is of the expected form.
The subject sequence contains no characters if the input
string is empty or consists entirely of white space, or if
the first non-white-space character is other than a sign or
a permissible letter or digit.
Here we may have a disagreement about whether a negative sign is a
permissable character for unsigned values. In either case the
2.03 version of strtoul does NOT return ULONG_MAX for -2, it
simply performs the usual conversion modulo ULONG_MAX+1. This is
definitely not standards conforming IMO, and makes a mockery of
overflow detection.
[#5] If the subject sequence has the expected form and the
value of base is zero, the sequence of characters starting
with the first digit is interpreted as an integer constant
according to the rules of 6.4.4.1. If the subject sequence
has the expected form and the value of base is between 2 and
36, it is used as the base for conversion, ascribing to each
letter its value as given above. If the subject sequence
begins with a minus sign, the value resulting from the
conversion is negated (in the return type). A pointer to
the final string is stored in the object pointed to by
endptr, provided that endptr is not a null pointer.
[#6] In other than the "C" locale, additional locale-
specific subject sequence forms may be accepted.
[#7] If the subject sequence is empty or does not have the
expected form, no conversion is performed; the value of nptr
is stored in the object pointed to by endptr, provided that
endptr is not a null pointer.
Returns
[#8] The strtol, strtoll, strtoul, and strtoull functions
return the converted value, if any. If no conversion could
be performed, zero is returned. If the correct value is
outside the range of representable values, LONG_MIN,
LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is
returned (according to the return type and sign of the
value, if any), and the value of the macro ERANGE is stored
in errno.
--
Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT worldnet DOT att DOT net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
- Raw text -