There was apparently a frantic bug-hunt involving “Project: Lumbergh” yesterday that I was not involved in. From the description of the bug, it certainly sounded like it was a manifestation of “undefined behavior” as “Project: Lumbergh” was actinging differently between Linux and Mac OS-X (our testing and development platforms). The issue stemmed from this bit of code (not the exact code, but similar in nature):
x = strtol(input,&endptr,10); if ((errno == 0) && (endptr != input)) /* ... */
And the fix:
errno = 0; /* BUG FIX */ x = strtol(input,&endptr,10); if ((errno == 0) && (endptr != input)) /* ... */
there are man pages that show setting
errno to 0 in sample code.
Here's what the C99 Standard says about library functions setting
The value of
errnois zero at program startup, but is never set to zero by any library function. The value of
errnomay be set to nonzero by a library function call whether or not there is an error, provided the use of
errnois not documented in the description of the function in this International Standard.
C99 Standard, section 7.5.3
So no function will ever set
errno to 0.
this sounds like some earlier failed function causing a false problem in this bit of code,
thus the fix to set
errno to 0.
the C99 Standard says this about
strtoullfunctions 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,
ULLONG_MAXis returned (according to the return type and sign of the value, if any), and the value of the macro
ERANGEis stored in
C99 Standard, section 126.96.36.199.8
on both Linux and Mac OS-X,
for values outside the given range,
ERANGE is returned.
The issue happens when no conversion happens.
Both systems return 0,
but Linux doesn't set
errno whereas Mac OS-X does.
Mac OS-X sets
which isn't even defined in the C99 Standard
(but it is defined for POSIX).
I think Mac OS-X has the wrong behavior here.
errno is documented in the description of
(but only for one error case),
so Mac OS-X shouldn't be (in my opinion)
errno when there's a conversion issue.
It may be a moot point though, as the fix appears to be working as intended on both systems.