GCC抱怨str不能为NULL

ive*_*eqy 5 c gcc-warning

我有以下代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

当试图编译它时

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
Run Code Online (Sandbox Code Playgroud)

我正在使用gcc版本:

fug@fugtop ~/p/book> gcc --version
gcc (Debian 6.3.0-18) 6.3.0 20170516
Run Code Online (Sandbox Code Playgroud)

我不明白这个警告.const char*应该可以为NULL,对吗?

Pet*_*ter 17

atoi是c库中的标准函数.该标准函数的标题包含该属性__nonnull,该属性触发特殊的gcc处理.

您重写的实现不会使用该特殊处理(遵循参数不为空的假设),因此警告.

真正的答案:不要从标准库中的函数重用函数名.