atoi — how to identify the difference between zero and error?

CAtoi

C Problem Overview


http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

Return Value

On success, the function returns the converted integral number as an int value. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

So how I differ between atoi("poop") and atoi("0") and atoi("0000000")

Yes I can loop and check for all zeroes in case I get 0 result, but isn't there a better way?

Notice: I use ANSI C89

C Solutions


Solution 1 - C

That's one of the reasons atoi is sometimes considered unsafe. Use strtol / strtoul instead. And if you have it use strtonum.

The function atoi is more dangerous than you might think. The POSIX standard says:

> If the value cannot be represented, the behavior is undefined.

The C99 standard says this also:

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

Solution 2 - C

As described by @cnicutar and @ouah, atoi can't distinguish a valid 0 from an invalid string making the strtol family better options.

But Why? and How? First understand that both atoi and strtol only convert the initial set of numbers in a string to numeric values. Any trailing non-numeric characters are simply ignored. strtol can be used to check for invalid strings because in addition to a numeric value, it also returns a pointer to the end of the numeric portion of the string. Thus if this end pointer still refers to the start of the original string, you can tell that there was an error and no characters from the string were converted.

There are a few of other subtleties, as seen in the code example:

long lnum;
int num;
char *end;

errno = 0;

lnum = strtol(in_str, &end, 10);        //10 specifies base-10
if (end == in_str)     //if no characters were converted these pointers are equal
	fprintf(stderr, "ERROR: can't convert string to number\n");

//If sizeof(int) == sizeof(long), we have to explicitly check for overflows
if ((lnum == LONG_MAX || lnum == LONG_MIN) && errno == ERANGE)	
	fprintf(stderr, "ERROR: number out of range for LONG\n");

//Because strtol produces a long, check for overflow
if ( (lnum > INT_MAX) || (lnum < INT_MIN) )
	fprintf(stderr, "ERROR: number out of range for INT\n");

//Finally convert the result to a plain int (if that's what you want)
num = (int) lnum;

Note: If you are sure the input string will be within the valid int range, you can eliminate lnum and simply cast strtol's return directly: num = (int) strtolen(in_str, &end, 10);

Solution 3 - C

You cannot.

atoi cannot detect errors. If the result cannot be represented, atoi invokes undefined behavior. Use strtol instead of atoi.

Secure CERT coding advises to use strtol instead of atoi, read:

INT06-C. Use strtol() or a related function to convert a string token to an integer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNahumView Question on Stackoverflow
Solution 1 - CcnicutarView Answer on Stackoverflow
Solution 2 - CBryan PView Answer on Stackoverflow
Solution 3 - CouahView Answer on Stackoverflow