Dan said:
In <
[email protected]> nrk
Isn't:
char s[] = "--4";
char *endptr;
strtol(s, &endptr, 0);
supposed to return 0 and set endptr to s?
Yes.
I have run into an implementation (not gcc, gcc does what I expect)
gcc has exactly zilch to do with your problem: it's only a compiler, not
a full implementation, and strtol() is a library function.
Sorry. That was sloppy on my part. I should've said glibc.
That implementation thinks that the correct result of converting "--4"
is outside the range of long. Check if errno is set to ERANGE, too.
Yes, it sets errno to ERANGE under this situation [*].
It consumes characters in the string like a regular strtol conversion and
sets endptr to the first non-digit encountered after that.
The implementation under question is the Intel C++ compiler suite v8.0.055
(invoked as a C compiler) under Linux
The behavior is truly bizzare. I have a nagging feeling that this
implementation is using the fact that integer overflow results in wrap
around (no harm in that, as the implementation very well knows this is the
case). Here's what it does:
"--123" : Returns LONG_MIN and sets errno to ERANGE
"-+123" : Returns -123 and errno is not set
"+-123" : Returns LONG_MAX and sets errno to ERANGE
"++123" : Returns 123 and errno is not set.
So far so good. But look what happens:
LONG_MIN = -2147483648, LONG_MAX = 2147483647
"--2147483647" : Returns LONG_MIN and sets errno to ERANGE
"--2147483648" : Returns LONG_MIN and errno is not set!!
"--2147483649" : Returns LONG_MIN + 1 and errno is not set!!
"-+2147483647" : Returns -LONG_MAX and errno is not set!!
"-+2147483648" : Returns LONG_MIN and errno is not set!!
"-+2147483649" : Returns LONG_MIN and sets errno to ERANGE
"+-2147483647" : Returns LONG_MAX and sets errno to ERANGE
"+-2147483648" : Returns LONG_MAX and sets errno to ERANGE
"+-2147483649" : Returns LONG_MAX and errno is not set!!
"--2147483647" : Returns LONG_MIN and sets errno to ERANGE
"--2147483648" : Returns LONG_MIN and errno is not set!!
"--2147483649" : Returns LONG_MIN + 1 and errno is not set!!
Even accounting for overflow, underflow and wrap-arounds, I am not able to
fully explain all those results.
Who was that who said "This is not correct. This is not even wrong!" again?
I posted a message on their public forums, but if someone has Intel Premier
support and experience this issue, I encourage you to file a bug report
with them through your premier support as well.
-nrk.