Re: Check all errors in code?



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.
.



Relevant Pages

  • Re: Help with changing char *string to int * string for sorting.
    ... > I'm trying to sort integers from a text file. ... otherwise the string may not appear. ... It looks as if you have the impression that atoi() would convert all ... int x; ...
    (comp.lang.c)
  • Re: Standard Library function converting char[] to int?
    ... |>function to convert a NTBS of digits to an integer? ... | Neither strtol nor strtod are "native C++ functions", ... | the string is too large. ... | int main ...
    (comp.lang.cpp)
  • Re: string to int
    ... >> atoi - Simplest but no error handling in case of bad input. ... > Well the thread started with a title such as "string to int" ...
    (microsoft.public.vc.language)
  • Re: string to int
    ... > atoi - Simplest but no error handling in case of bad input. ... Well the thread started with a title such as "string to int" ... So "int atoi" is as simple as it gets. ...
    (microsoft.public.vc.language)
  • Re: atoi query
    ... have noticed the problem with your argument to atoi. ... strtol) must be supplied with a string, not a character, for converting ... lineis not a string, ... a sequence of characters terminated by the first null character. ...
    (comp.lang.c)