错误:strncat-param-overlap

地址清理器错误:strncat-param-overlap

在重叠缓冲区中移动内存的代码可能会导致难以诊断的错误。

示例

此示例演示 AddressSanitizer 如何捕获由到 CRT 函数的重叠参数导致的错误。

(基于 llvm-project/compiler-rt/test/asan/TestCases/strncat-overlap.cpp。)

// example1.cpp
// strncat-param-overlap error
#include <string.h>

void bad_function() {
    char buffer[] = "hello\0XXX";
    strncat(buffer, buffer + 1, 3); // BOOM
    return;
}

int main(int argc, char **argv) {
    bad_function();
    return 0;
}

若要生成并测试此示例,请在 Visual Studio 2019 版本 16.9 或更高版本的开发人员命令提示符中运行以下命令:

cl example1.cpp /fsanitize=address /Zi
devenv /debugexe example1.exe

生成的错误

示例 1 中显示 strncat-param-overlap 错误的调试器的屏幕快照。

另请参阅

AddressSanitizer 概述
AddressSanitizer 已知问题
AddressSanitizer 生成和语言参考
AddressSanitizer 运行时参考
AddressSanitizer 阴影字节
AddressSanitizer 云或分布式测试
AddressSanitizer 调试程序集成
AddressSanitizer 错误示例