c++ - 为什么内存 sanitizer 报告使用了 std::map 的未初始化值?

标签 c++ clang memory-sanitizer

我在 x86-64 上使用 manjaro linux。 Memory-sanitizer in clang version 10.0.1 reported a use of uninitialized value error in std::map,这让我很惊讶。我做错了什么吗?

$ cat test.cpp 
#include <map>
int main() {
    std::map<int, int> test;
    test.insert({1,2});
}
$ clang++ -fsanitize=memory test.cpp && ./a.out
==51936==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x562889eaad9a  (/tmp/build/a.out+0x9fd9a)
    #1 0x562889eaae28  (/tmp/build/a.out+0x9fe28)
    #2 0x562889eaaba1  (/tmp/build/a.out+0x9fba1)
    #3 0x562889eaa51e  (/tmp/build/a.out+0x9f51e)
    #4 0x562889eaa087  (/tmp/build/a.out+0x9f087)
    #5 0x7f418e02b151  (/usr/lib/libc.so.6+0x28151)
    #6 0x562889e2b1dd  (/tmp/build/a.out+0x201dd)

SUMMARY: MemorySanitizer: use-of-uninitialized-value (/tmp/build/a.out+0x9fd9a) 
Exiting

最佳答案

FWIW 看起来 libc++ 比 stdlibc++ 对 MSAN 更友好,因为编译一个类似的

#include <map>
#include <string>

int main(int argc, char** argv) {
    std::map<int, std::string> m;
    m[argc] = argv[argc - 1];
    return 0;
}

用后者编写代码并运行

% clang++ -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cpp

导致类似的错误,但是做

% clang++ -fsanitize=memory -fno-omit-frame-pointer -stdlib=libc++ -g -O2 umr.cpp && ./a.out

工作正常(clang 13,Debian Sid)。

关于c++ - 为什么内存 sanitizer 报告使用了 std::map 的未初始化值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63947483/

相关文章:

c++ - 错误 : Invalid use of void

c++ - 从线中提取坐标数组 (C++ OpenCV)

c++ - 缩小从 double 到 float 的转换范围

c++ - Clang 与 MSVC : Treatment of template function prototypes

c++ - 如何实际使用 AddressSanitizer 和 MemorySanitizer?

c++ - CMake:有没有强制链接库?

c++ - 如何从 C++ std::basic_ostream 派生并使 << 运算符虚拟?

python - 安装枕头错误

c++ - 使用 Memory Sanitizer 检测 libc++