c++ - 将内存清理程序与 libstdc++ 一起使用

标签 c++ clang++ libstdc++ msan memory-sanitizer

我希望在 clang 中使用 -fsanitize=memory 标志来分析如下程序:

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void writeToFile(){
    ofstream o;
    o.open("dum");
    o<<"test"<<endl; //The error is here.
                     //It does not matter if the file is opened this way,
                     //or with o("dum");
    o.close();
}
int main(){
    writeToFile();
}

据我所知,这个程序是正确的,但是当我使用 clang++ san.cpp -fsanitize=memory 它失败(在运行时):

UMR in __interceptor_write at offset 0 inside [0x64800000e000, +5)  
==9685== WARNING: MemorySanitizer: use-of-uninitialized-value  
    #0 0x7f48d0899ae5 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x7bae5)  
    #1 0x7f48d08d1787 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb3787)  
    #2 0x7f48d08d21e2 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb41e2)  
    #3 0x7f48d08cfd1e (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb1d1e)  
    #4 0x7f48d08b1f2d (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x93f2d)  
    #5 0x7f48d16d60f5 in writeToFile() /home/daniel/programming/test/santest.cpp:10  
    #6 0x7f48d16d61f4 in main /home/daniel/programming/test/santest.cpp:15  
    #7 0x7f48d0261de4 (/lib/x86_64-linux-gnu/libc.so.6+0x21de4)  
    #8 0x7f48d16d5e42 in _start (/home/daniel/programming/test/a.out+0x61e42)  

SUMMARY: MemorySanitizer: use-of-uninitialized-value ??:0 ??

我怎样才能使它正常工作?

Clang 3.5 版,stdlibc++ 6 版

最佳答案

代码当然没问题,但许多类似的错误是由clang的内存清理工具的以下要求引起的:

MemorySanitizer (without a dynamic component) requires that the entire program code including libraries, (except libc/libm/libpthread, to some extent), is instrumented.

from here

您正在使用 libstdc++ 的 cplusplus 运行时未经测试并导致错误。不幸的是,您将不得不按照该链接中描述的一些繁琐的过程来重建已检测的 libstdc++ 或切换到 libc++(更简单)

关于c++ - 将内存清理程序与 libstdc++ 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617788/

相关文章:

c++ - 通过按位选择删除分支

C++11 lambda 不通过引用获取 const 变量,为什么?

c++ - std::reduce 与仿函数

c++ - 将 g++ 4.8 链接到 libstdc++

c++ - 流式传输 stringstream 是 libstdc++ 扩展吗?

c++ - 使用模板参数指定策略

c++ - 如何使用 sol2 编写包含 lua 表的文件

c++ - 提高 Tesseract 检测质量

c++ - 浮点舍入为 80 位寄存器和 64 位 double : ill-formed code or gcc/clang bug? 提供不同的结果

c++ - 存储在变量模板特化中的 Spirit-X3 解析器不适用于 Clang