GCC complains str cannot be NULL

I have the following code:

int atoi(const char * str) { int ret = 0, i; if (str) { for (i = 0; str[i] != '\0'; i++) if (str[i] >= '0' && str[i] <= '9') ret = ret * 10 + str[i] - '0'; } return ret; } 

when trying to compile it with

 fug@fugtop ~/p/book> make gcc -c -o db.o db.c -Wall -Werror -std=c99 -g -DVERSION=\"v0.4\" -Wno- unused-variable -Wno-unused-function gcc -c -o misc.o misc.c -Wall -Werror -std=c99 -g -DVERSION=\"v0.4\" - Wno-unused-variable -Wno-unused-function misc.c: In function 'atoi': misc.c:55:5: error: nonnull argument 'str' compared to NULL [-Werror=nonnull-compare] if (str) { ^ cc1: all warnings being treated as errors Makefile:54: recipe for target 'misc.o' failed make: *** [misc.o] Error 1 

I am using gcc version:

 fug@fugtop ~/p/book> gcc --version gcc (Debian 6.3.0-18) 6.3.0 20170516 

I do not understand the warning. A const char * must be NULL, right?

+5
source share
1 answer

atoi is a standard feature in the c library. The header of this standard function contains the __nonnull attribute, which runs gcc special processing.

Your overridden implementation does not use this special handling (observing the assumption that the argument is not null), so a warning.

The real answer: do not use the function name from the function from the standard library.

+17
source

Source: https://habr.com/ru/post/1271690/


All Articles