c - 带有 -D_FORTIFY_SOURCE=2 的 swprintf 引发缓冲区溢出
问题描述
这是我第一次使用 wchar,我发现了一些令人惊讶的地方。我找不到答案,所以我将分享我的经验。
我有一个简单的测试程序(基于 swprintf 示例)
#include <stdio.h>
#include <wchar.h>
int main()
{
wchar_t str_unicode[100] = {0};
swprintf(str_unicode, sizeof(str_unicode), L"%3d\n", 120);
fputws(str_unicode, stdout);
return 0;
}
在有或没有优化的情况下编译它都可以正常工作:
gcc -O2 test.c -o test
运行它也可以正常工作:
./test
120
但是在我当前的项目中,我使用-D_FORTIFY_SOURCE=2
,它使这个简单的程序崩溃:
gcc -O2 -D_FORTIFY_SOURCE=2 test.c -o test
./test
*** buffer overflow detected ***: terminated
[1] 28569 IOT instruction (core dumped) ./test
我有更多关于 valgrind 的详细信息,但似乎__swprintf_chk
失败了。
valgrind ./test
==30068== Memcheck, a memory error detector
==30068== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30068== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==30068== Command: ./test
==30068==
*** buffer overflow detected ***: terminated
==30068==
==30068== Process terminating with default action of signal 6 (SIGABRT): dumping core
==30068== at 0x48A29E5: raise (in /usr/lib64/libc-2.32.so)
==30068== by 0x488B8A3: abort (in /usr/lib64/libc-2.32.so)
==30068== by 0x48E5006: __libc_message (in /usr/lib64/libc-2.32.so)
==30068== by 0x4975DF9: __fortify_fail (in /usr/lib64/libc-2.32.so)
==30068== by 0x4974695: __chk_fail (in /usr/lib64/libc-2.32.so)
==30068== by 0x49752C4: __swprintf_chk (in /usr/lib64/libc-2.32.so)
==30068== by 0x401086: main (in /home/pierre/workdir/test)
==30068==
==30068== HEAP SUMMARY:
==30068== in use at exit: 0 bytes in 0 blocks
==30068== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==30068==
==30068== All heap blocks were freed -- no leaks are possible
==30068==
==30068== For lists of detected and suppressed errors, rerun with: -s
==30068== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
[1] 30068 IOT instruction (core dumped) valgrind ./test
我不明白为什么这个检查会失败,缓冲区大小对于单个整数来说已经足够了(100)。它是libc中的错误吗?还是我做错了什么?
我的 GCC 版本是 10.3.1
gcc --version
gcc (GCC) 10.3.1 20210422 (Red Hat 10.3.1-1)
解决方案
你的问题是函数调用的第二个参数 -
swprintf(str_unicode, sizeof(str_unicode), L"%3d\n", 120);
您传入了整个数组的字节大小- 即 400 字节 if sizeof(wchar_t) == 4
。
但是swprintf
的第二个参数是wchar_t 数组中的单元格数- 即在您的示例中为 100 个单元格。
将您的函数调用更改为:
swprintf(str_unicode, sizeof(str_unicode) / sizeof(wchar_t), L"%3d\n", 120);
推荐阅读
- swift - HStack 和 TextField 的角半径未更新
- javascript - 用户在 Vue.js 中成功提交表单时如何清除表单输入字段?
- ckeditor - ckeditor cdn - 已经有值的文本区域不允许点击
- excel - 根据单元格值选择一个范围并将其复制到另一个工作簿中
- angular - 如何在 typescript 和 angular 11 中自动将数据从字符串转换为数字?
- sql - 显示空白值
- r - R:字符串向量的平均成对差异
- javascript - 使用 Vue 处理使用 vue-svg-loader 加载的 svg 形状上的点击事件
- android - 通过 Retrofit2 从 API 接收 ByteArray 数据会导致我的 Android 应用程序出错
- haskell - 擦除类型参数的 GADT 的相等性