Re: Check all errors in code?
- From: vippstar@xxxxxxxxx
- Date: Fri, 19 Sep 2008 10:24:44 -0700 (PDT)
On Sep 19, 7:51 pm, Keith Thompson <ks...@xxxxxxx> wrote:
Richard Heathfield <r...@xxxxxxxxxxxxxxx> writes:
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.
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.
[...]
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(nptr, (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.
A side note: Yes, the (char**) cast on the second argument to strtol
in the definition of atoi is necessary. Even in C99, it's still legal
(though unwise) to call strtol without a visible prototype:
/* no #include <stdlib.h> */
long int strtol();
strtol(nptr, NULL, 0); /* UB */
Here NULL is not of type char**, and it won't be converted to char**,
causing undefined behavior.
Ah thanks, this bugged me for some time.
.
- Follow-Ups:
- Re: Check all errors in code?
- From: Jack Klein
- Re: Check all errors in code?
- References:
- Check all errors in code?
- From: lovecreatesbea...@xxxxxxxxx
- Re: Check all errors in code?
- From: Richard Heathfield
- Re: Check all errors in code?
- From: Keith Thompson
- Re: Check all errors in code?
- From: Richard Heathfield
- Re: Check all errors in code?
- From: Keith Thompson
- Check all errors in code?
- Prev by Date: Re: Can anyone help me with this code
- Next by Date: Initialization of const variables and C standards (Was: Initialization of a const matrix implemented as pointer-to-pointer)
- Previous by thread: Re: Check all errors in code?
- Next by thread: Re: Check all errors in code?
- Index(es):
Relevant Pages
|