C - _FORTIFY_SOURCE 宏在 gcc 上创建奇怪的警告

问题描述 投票:0回答:1

我目前在 arch linux 上使用 gcc 14,并在尝试使用宏 _FORTIFY_SOURCE 编译我的 C 程序时,显示此警告:

In file included from /usr/include/stdio.h:970,
                 from src/logger.c:2:
In function 'vsnprintf',
    inlined from 'logOutput' at src/logger.c:24:5:
/usr/include/bits/stdio2.h:100:10: warning: '__builtin___vsnprintf_chk' specified bound 32753 exceeds destination size 32752 [-Wstringop-overflow=]
  100 |   return __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101 |                                     __glibc_objsize (__s), __fmt, __ap);
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我真的不知道发生了什么,我唯一知道的是,当我省略宏时它就会消失,并且警告出现在所有 3 个级别上。

c gcc-warning
1个回答
0
投票

我发现问题了。这是我的代码中有问题的行:

vsnprintf(&buffer[max_len_level_string], BUFFER_SIZE - (max_len_level_string + 1), format, args);

我忘记了括号,因此缓冲区可能会溢出 1。这个警告理解起来很奇怪,但它完成了它的工作,并警告我,因为它应该......

抱歉我的错误,要关闭这篇文章了。

© www.soinside.com 2019 - 2024. All rights reserved.