ISO C委员会(ISO/IEC JTC1/SC21/WG14)已发布TR 24731-1,正在研究TR 24731-2:
TR 24731-1:C库的扩展第一部分:边界检查接口
WG14正在研究更安全的C库函数.该TR旨在通过添加具有缓冲区长度的额外参数来修改现有程序.最新草案见N1225号文件.理由是在N1173号文件中.这将成为技术报告类型2.
TR 24731-2:C库的扩展 - 第二部分:动态分配功能
WG14正在研究更安全的C库函数.该TR面向使用动态分配而不是缓冲区长度的额外参数的新程序.最新草案见N1337号文件.这将成为技术报告类型2.
我确信编码中存在很多缓冲区溢出风险,其中许多风险是通过标准库的“_s”安全函数解决的。尽管如此,我发现自己有时对其中一些感到困惑。
假设我有一些这样的片段
uint8_t a[5];
...
size_t z = 6;
...
memset(a, 0, z); // Overflow!
Run Code Online (Sandbox Code Playgroud)
一些编译器(C11)可能建议更好地使用memset_s; 因为我是一个糟糕的程序员,所以我刚刚将我的代码更新为这个全新的东西,我的方式:
uint8_t a[5];
...
rsize_t max_array = 56; // Slipped finger, head in the clouds, etc.
rsize_t z = 6;
...
memset_s(a, max_array, 0, z); // So what?
Run Code Online (Sandbox Code Playgroud)
如果我只是将错误添加到另一个参数,那会memset_s更好吗?memset
增加的安全性在哪里,如果添加一个新参数只是添加一个可能会出错的新参数。我本可以在第一个修订版中更正我的代码,并且仍然可以合法地调用缓冲区上定义良好的操作。
抛开带有未经检查的零指针的情况,怎么会memset比更糟糕呢memset_s?
[编辑]
经过一番努力,我还发现了导致我的设置中出现警告的设置。这可能对某人有帮助。
该警告来自 Clang-tidy,在 Visual Studio 扩展“Clang power tools”中调用。在其默认设置中,它启用了 Clang-tidy检查器“ security.insecureAPI.DeprecatedOrUnsafeBufferHandling ”,它模仿默认的 …
I'm running OS X Sierra and trying to compile a c program that uses strcpy_s, but my installed clang compiler is using the c99 standard, but from what I've read strcpy_s requires c11.
Here's the code I'm trying to compile
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char source[] = "Test string";
char destination[50];
if(strcpy_s(destination, sizeof(destination), source))
printf("string copied - %s",destination);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
And here's the command I'm using to compile
$ clang copytest.c -o copytest …Run Code Online (Sandbox Code Playgroud)