2016-01-20 129 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

這很好。謝謝:)我知道我有時會嘗試一些特殊的「黑客」組合,但對於真實世界的應用程序,我避免了這種情況。不管怎樣,謝謝!:) –