相关疑难解决方法(0)

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

我希望使用-fsanitize=memoryclang中的标志来分析如下的程序:

#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();
}
Run Code Online (Sandbox Code Playgroud)

据我所知,这个程序是正确的,但是当我使用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 …
Run Code Online (Sandbox Code Playgroud)

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

28
推荐指数
2
解决办法
3300
查看次数

C字符串,strlen和Valgrind

我试图理解为什么Valgrind正在吐痰:

==3409== Invalid read of size 8
==3409==    at 0x4EA3B92: __GI_strlen (strlen.S:31)
Run Code Online (Sandbox Code Playgroud)

每当我在动态分配的字符串上应用strlen时?

这是一个简短的测试用例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

  char *hello = "Hello World";
  char *hello2;

  /* Step 1 */
  printf("Step 1\n");
  printf("strlen : %lu\n",(unsigned long)strlen(hello));

  /* Step 2 */
  hello2 = calloc(12,sizeof(char));
  hello2[0] = 'H';
  hello2[1] = 'e';
  hello2[2] = 'l';
  hello2[3] = 'l';
  hello2[4] = 'o';
  hello2[5] = ' ';
  hello2[6] = 'W';
  hello2[7] = 'o';
  hello2[8] = 'r';
  hello2[9] = 'l';
  hello2[10] = …
Run Code Online (Sandbox Code Playgroud)

c string valgrind strlen

7
推荐指数
1
解决办法
5633
查看次数

标签 统计

c ×1

c++ ×1

clang++ ×1

libstdc++ ×1

memory-sanitizer ×1

msan ×1

string ×1

strlen ×1

valgrind ×1