2016-01-20 126 views
1

我有一个gcc编译问题。gcc:__fread_chk_warn警告

假设下面的程序:

#include <stdio.h> 

int test(const char *fname) { 
    FILE *fh = fopen(fname, "rb"); 
    int tmp; 
    if (fread(&tmp, sizeof(tmp), 1, fh) < 1) { 
     tmp = 0; 
    } 
    fclose(fh); 
    return tmp; 
} 

int main(void) { 
    printf("%d\n", test("test.txt")); 
    return 0; 
} 

和文件test.txt

11111111111111111111111111111111... 

当然,该计划是非常愚蠢的,但它的工作原理:

[email protected]:~/tmp/optxx$ gcc -O3 -Wall -Wextra test.c 
[email protected]:~/tmp/optxx$ ./a.out 
825307441 

让我们修改它有点(只添加一个属性到test功能):

#include <stdio.h> 

int __attribute__((optimize("O0"))) test(const char *fname) { 
    FILE *fh = fopen(fname, "rb"); 
    int tmp; 
    if (fread(&tmp, sizeof(tmp), 1, fh) < 1) { 
     tmp = 0; 
    } 
    fclose(fh); 
    return tmp; 
} 

int main(void) { 
    printf("%d\n", test("test.txt")); 
    return 0; 
} 

功能test不应再进行优化。但现在编译失败:

[email protected]:~/tmp/optxx$ gcc -Wall -Wextra -O3 test.c 
In file included from /usr/include/stdio.h:936:0, 
       from test.c:1: 
In function ‘fread’, 
    inlined from ‘test’ at test.c:6:9: 
/usr/include/x86_64-linux-gnu/bits/stdio2.h:293:9: warning: call to ‘__fread_chk_warn’ declared with attribute warning: fread called with bigger size * nmemb than length of destination buffer 
    return __fread_chk_warn (__ptr, __bos0 (__ptr), __size, __n, __stream); 
     ^

我得到一个警告,通常我-Werror编译,所以我不喜欢的警告。 所使用的gcc版本:

[email protected]:~/tmp/optxx$ gcc -v 
Using built-in specs. 
COLLECT_GCC=gcc 
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper 
Target: x86_64-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.3.1-6ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu 
Thread model: posix 
gcc version 5.3.1 20160119 (Ubuntu 5.3.1-6ubuntu2) 

不幸的是我不能够解决警告:(

也许有人有一个想法,为什么发生这种情况

CNC中

A(?脏)破解也可以删除这个警告:)

-edit-

可能是我的linux的东西是错的,因为它似乎适用于其他人。 Anayway一个肮脏的黑客/修复将是fread调用之前添加以下代码:

static size_t fread_wrapper(void *ptr, size_t size, size_t count, FILE *stream) { 
    return fread(ptr, size, count, stream); 
} 
#define fread(ptr, size, count, stream)   fread_wrapper((ptr), (size), (count), (stream)) 
+0

gcc 4.4.5没有错误。 –

+0

不幸的是,我也遇到了'gcc 4.6.4'的错误。我希望它不是gcc的错误。 –

+0

用gcc 5.3.0适合我! –

回答

1

Ubuntu的启用优化级别-O1或更高_FORTIFY_SOURCE功能默认情况下。此选项使系统标题预处理可使用fread等不同的功能。这些功能对参数进行一些基本的安全检查。当包含头文件时,该选项会在全局启用,因此它假定所有代码都将被编译为-O1或更高版本。我假设它在较低的优化级别上被禁用,因为gcc没有足够的信息而没有进行一些优化,并且导致这些魔法强化宏来产生误报(就像你得到的那样)。

如果你用-O0编译整个文件,_FORTIFY_SOURCE被禁用,事情就会发挥作用。或者我猜你可以用-D_FORTIFY_SOURCE=0编译整个文件,虽然我还没有尝试过。

此外,我可以通过添加-D_FORTIFY_SOURCE=1在其他版本的linux上重现此操作。

我想你可以称之为编译器/ glibc/Ubuntu的bug。或者停止使用疯狂的优化属性。世界不能用所有可能的奇怪组合进行测试,所以我们在按下按钮和旋转旋钮时应该小心。

+0

这很好。谢谢:)我知道我有时会尝试一些特殊的“黑客”组合,但对于真实世界的应用程序,我避免了这种情况。不管怎样,谢谢!:) –