G
Georg Peter
Many functions define errno values. This does NOT imply
that errno will be left unchanged when a function
succeeds.[#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.It does not state that errno stays unchanged when the
function succeeds. So errno could have the value ERANGE
altough the function succeeds. Assuming otherwise means
you depend on undefined behaviour. IMHO you need to
check forzero, LONG_MIN and LONG_MAX for strtol()
zero, LLONG_MIN and LLONG_MAX for strtoll()
zero, ULONG_MAX for strtoul()
zero, ULLONG_MAX for strtoull()and only when the function returns one of the values
mentioned above errno will have a defined value.For me also, see above.Georg Peter
Hmmm. Again, the errno definition:
[#3] The value of errno is zero at program startup, but is
never set to zero by any library function.159) The value of
errno may be set to nonzero by a library function call
whether or not there is an error, provided the use of errno
is not documented in the description of the function in this
International Standard.
I believe this clearly means that *IF* a function in any way documents
its use of errno, it is constrained to only set it to non-zero under
the conditions documented.
In other words: When errno is mentioned in the function
description you assume that errno stays unchanged when
the function succeeds.
This is WRONG because of the following reasons:
1. Many function descriptions contain phrases like:
... in case of error ... is stored in errno.
IMHO this does NOT mean that "the use of errno is
documented" for this function. It just specifies the
values errno will have in case of error. E.g.:
The manpage of fopen() contains the sentence:
Upon successful completion fopen(), fdopen() and
freopen() return a FILE pointer. Otherwise, NULL
is returned and errno is set to indicate the error.
But fopen() is definitely NOT a function where you can
check errno to find out if fopen() succeeded. With your
logic the success of many other library functions
could be checked by ckecking errno.
2. Your description of strtol, strtoll, strtoul and
strtoull mentiones errno only once. And it just
says that errno gets the value ERANGE when the
correct value is outside the range of representable
values. It does NOT say that errno is left unchanged
when the function succeeds.
By your logic, seems to me that the clause
"provided the use of..." in the errno
definition is rendered effectively meaningless.
No, the clause "provided the use of..." is NOT
meaningless. IMHO
If the correct value is outside the range of
representable values ... the value of the macro
ERANGE is stored in errno.
is NOT a sentence which describes the use of errno,
since it does NOT define what happens with errno
when the function succeeds.
Describing the USE of errno would be much more.
The USE of errno could be described with an example
like:
errno = 0;
function();
if (errno == 0) {
...
But such a use example is NOT present for strtoul().
Just describing a possible value for errno does
NOT describe the use of errno.
Georg Peter