瓦尔格林德无法识别无效写入

valgrind doesn't recognize invalid write

本文关键字:无效 识别 林德无      更新时间:2023-10-16

>Valgring 不会检测到内存错误。

我正在使用 valgrind 3.11、gcc 5.4.0 在 ubuntu 和 我的程序中的代码不正确,如示例中所示。

我使用valgrind分析了这个程序。但瓦尔格林德没有报告任何错误。

  #include <string.h>
   int main(){
     int a[3];
     memcpy(a,"aaabbbcccdddeeefffggghhh", 24);
     return 0;
   }

瓦尔格林德怎么了?

valgrind 不知道 a,不知道它的大小,当你留在堆栈中时,它无法检测到错误


相比之下,有:

#include <string.h>
int main(){
  int * a = new int[3];
  memcpy(a,"aaabbbcccdddeeefffggghhh", 24);
  return 0;
}

Valgrind可以检测到错误,因为它知道分配块的大小:

pi@raspberrypi:/tmp $ valgrind ./a.out
==16164== Memcheck, a memory error detector
==16164== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==16164== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==16164== Command: ./a.out
==16164== 
==16164== Invalid write of size 8
==16164==    at 0x4865F44: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==16164==  Address 0x4bc9f60 is 8 bytes inside a block of size 12 alloc'd
==16164==    at 0x48485F0: operator new[](unsigned int) (vg_replace_malloc.c:417)
==16164==    by 0x105A7: main (v.cc:3)
==16164== 
==16164== Invalid write of size 8
==16164==    at 0x4865F54: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==16164==  Address 0x4bc9f68 is 4 bytes after a block of size 12 alloc'd
==16164==    at 0x48485F0: operator new[](unsigned int) (vg_replace_malloc.c:417)
==16164==    by 0x105A7: main (v.cc:3)
==16164== 
==16164== 
==16164== HEAP SUMMARY:
==16164==     in use at exit: 12 bytes in 1 blocks
==16164==   total heap usage: 2 allocs, 1 frees, 20,236 bytes allocated
==16164== 
==16164== LEAK SUMMARY:
==16164==    definitely lost: 12 bytes in 1 blocks
==16164==    indirectly lost: 0 bytes in 0 blocks
==16164==      possibly lost: 0 bytes in 0 blocks
==16164==    still reachable: 0 bytes in 0 blocks
==16164==         suppressed: 0 bytes in 0 blocks
==16164== Rerun with --leak-check=full to see details of leaked memory
==16164== 
==16164== For counts of detected and suppressed errors, rerun with: -v
==16164== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 3)