gcc: __fread_chk_warn 警告
gcc: __fread_chk_warn warning
我有 gcc 的编译问题。
假设程序如下:
#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...
当然,这个程序很笨,但它确实有效:
user@ubuntu:~/tmp/optxx$ gcc -O3 -Wall -Wextra test.c
user@ubuntu:~/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
。但是现在编译失败:
user@ubuntu:~/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
版本:
user@ubuntu:~/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)
不幸的是我无法修复警告:(
也许有人知道为什么会这样?
-编辑-
(肮脏的)hack 也可以删除此警告:)
-编辑-
可能我的 linux 有什么地方不对,因为它似乎对其他人都有效。无论如何,一个肮脏的破解/修复是在 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))
Ubuntu 默认为 -O1
或更高的优化级别启用 _FORTIFY_SOURCE
功能。此选项使系统 headers pre-process 为 fread
等使用不同的功能。这些函数执行一些基本的参数安全检查。当包含 header 时,该选项将全局启用,因此它假设所有代码都将使用 -O1
或更高版本进行编译。我假设它在较低的优化级别上被禁用,因为 gcc 在没有一些优化的情况下没有足够的信息,这会导致那些神奇的 fortify 宏产生误报(就像你得到的那样)。
如果您使用 -O0
编译整个文件,_FORTIFY_SOURCE
将被禁用并且一切正常。或者我想你可以用 -D_FORTIFY_SOURCE=0
编译整个文件,虽然我没有试过。
此外,我可以通过添加 -D_FORTIFY_SOURCE=1
.
在 linux 的其他版本上重现这一点
我想你可以称之为 compiler/glibc/Ubuntu 错误。或者干脆停止使用疯狂的优化属性。世界不能用所有可能的奇怪组合来测试,所以我们在按下按钮和转动旋钮时要小心。
我有 gcc 的编译问题。
假设程序如下:
#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...
当然,这个程序很笨,但它确实有效:
user@ubuntu:~/tmp/optxx$ gcc -O3 -Wall -Wextra test.c
user@ubuntu:~/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
。但是现在编译失败:
user@ubuntu:~/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
版本:
user@ubuntu:~/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)
不幸的是我无法修复警告:(
也许有人知道为什么会这样?
-编辑-
(肮脏的)hack 也可以删除此警告:)
-编辑-
可能我的 linux 有什么地方不对,因为它似乎对其他人都有效。无论如何,一个肮脏的破解/修复是在 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))
Ubuntu 默认为 -O1
或更高的优化级别启用 _FORTIFY_SOURCE
功能。此选项使系统 headers pre-process 为 fread
等使用不同的功能。这些函数执行一些基本的参数安全检查。当包含 header 时,该选项将全局启用,因此它假设所有代码都将使用 -O1
或更高版本进行编译。我假设它在较低的优化级别上被禁用,因为 gcc 在没有一些优化的情况下没有足够的信息,这会导致那些神奇的 fortify 宏产生误报(就像你得到的那样)。
如果您使用 -O0
编译整个文件,_FORTIFY_SOURCE
将被禁用并且一切正常。或者我想你可以用 -D_FORTIFY_SOURCE=0
编译整个文件,虽然我没有试过。
此外,我可以通过添加 -D_FORTIFY_SOURCE=1
.
我想你可以称之为 compiler/glibc/Ubuntu 错误。或者干脆停止使用疯狂的优化属性。世界不能用所有可能的奇怪组合来测试,所以我们在按下按钮和转动旋钮时要小心。