Re: Check all errors in code?
On Fri, 19 Sep 2008 10:24:44 -0700 (PDT), vippstar@gmail. com wrote in
comp.lang.c:
You are laboring under a dangerous misunderstandin g here.
Here is what C90 says about atoi(), atol(), and atof():
"The functions atof, atoi, and atol need not affect the value of the
integer expression errno on an error. If the value of the result
cannot be represented, the behavior is undefined."
The difference in C99 is that the new function atoll() is included:
"The functions atof, atoi, atol, and atoll need not affect the value
of the integer expression errno on an error. If the value of the
result cannot be represented, the behavior is undefined."
The ato... functions all invoke undefined behavior if the text string
represents a value outside the range of the function's return type. No
implementation-defined behavior or implementation-defined signal, just
plain old UB.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
On Fri, 19 Sep 2008 10:24:44 -0700 (PDT), vippstar@gmail. com wrote in
comp.lang.c:
On Sep 19, 7:51 pm, Keith Thompson <ks...@mib.orgw rote:
>
I believe the "behavior on error" is when the string is not an
integer.
strtol can not invoke undefined behavior. If atoi is equal to that
strtol call, it can't invoke undefined behavior if strtol returns 0.
It will invoke implementation defined behavior or will raise
implementation defined signal (only in C99) if the value returned by
strtol is < INT_MIN or INT_MAX.
Richard Heathfield <r...@see.sig.i nvalidwrites:
[...]
[...]
Good point; I hadn't realized that.
The following is a paraphrase of C99 7.20.1.2 (dropping the references
to atol and atoll for simplicity):
Description
The atoi function converts the initial portion of the string
pointed to by nptr to int representation. Except for the behavior
on error, it is equivalent to
(int)strtol(npt r, (char**)NULL, 10)
Returns
The atoi function returns the converted value.
And a paraphrase from C99 7.20.1.4:
The strtol function returns the converted value, if any. If no
conversion could be performed, zero is returned.
Until a moment ago, I assumed that the *intent* was that atoi()
returns 0 on error, and the standard just didn't express that intent
properly. But that intent could have been expressed simply by
dropping the phrase "Except for the behavior on error". Since it
doesn't say what the behavior on error *is*, the behavior is
undefined.
Keith Thompson said:
>Conceivably the code doesn't need to differentiate between a string
>representing 0 and a string that doesn't represent any integer value,
>and whatever code calls it will treat either as an error.
>representing 0 and a string that doesn't represent any integer value,
>and whatever code calls it will treat either as an error.
Whilst 0 is a common return value for atoi when its input can't be
represented as an int, the Standard doesn't mandate this, so code that
relies on it is broken.
represented as an int, the Standard doesn't mandate this, so code that
relies on it is broken.
Good point; I hadn't realized that.
The following is a paraphrase of C99 7.20.1.2 (dropping the references
to atol and atoll for simplicity):
Description
The atoi function converts the initial portion of the string
pointed to by nptr to int representation. Except for the behavior
on error, it is equivalent to
(int)strtol(npt r, (char**)NULL, 10)
Returns
The atoi function returns the converted value.
And a paraphrase from C99 7.20.1.4:
The strtol function returns the converted value, if any. If no
conversion could be performed, zero is returned.
Until a moment ago, I assumed that the *intent* was that atoi()
returns 0 on error, and the standard just didn't express that intent
properly. But that intent could have been expressed simply by
dropping the phrase "Except for the behavior on error". Since it
doesn't say what the behavior on error *is*, the behavior is
undefined.
I believe the "behavior on error" is when the string is not an
integer.
strtol can not invoke undefined behavior. If atoi is equal to that
strtol call, it can't invoke undefined behavior if strtol returns 0.
It will invoke implementation defined behavior or will raise
implementation defined signal (only in C99) if the value returned by
strtol is < INT_MIN or INT_MAX.
Here is what C90 says about atoi(), atol(), and atof():
"The functions atof, atoi, and atol need not affect the value of the
integer expression errno on an error. If the value of the result
cannot be represented, the behavior is undefined."
The difference in C99 is that the new function atoll() is included:
"The functions atof, atoi, atol, and atoll need not affect the value
of the integer expression errno on an error. If the value of the
result cannot be represented, the behavior is undefined."
The ato... functions all invoke undefined behavior if the text string
represents a value outside the range of the function's return type. No
implementation-defined behavior or implementation-defined signal, just
plain old UB.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
Comment