Valgrind错过了错误

时间:2016-05-03 06:51:10

标签: c valgrind

(原帖是here

考虑以下明显错误的计划:

#include <string.h>

int main()
{
  char string1[10] = "123456789";
  char *string2 = "123456789";

  strcat(string1, string2);
}

并假设编译它:

gcc program.c -ggdb

并在其上运行valgrind:

valgrind --track-origins=yes --leak-check=yes --tool=memcheck --read-var-info=yes  ./a.out

结果中没有显示错误:

==29739== Memcheck, a memory error detector
==29739== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==29739== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==29739== Command: ./a.out
==29739== 
==29739== 
==29739== HEAP SUMMARY:
==29739==     in use at exit: 0 bytes in 0 blocks
==29739==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==29739== 
==29739== All heap blocks were freed -- no leaks are possible
==29739== 
==29739== For counts of detected and suppressed errors, rerun with: -v
==29739== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

我错过了什么?

1 个答案:

答案 0 :(得分:5)

它没有报告任何错误,因为您使用的是foo(),它不对全局或堆栈数组执行检查,它只对堆数组执行边界检查和使用后检查。所以在这种情况下,您可以使用valgrind SGCheck来检查堆栈数组:

memcheck

它确实为我报告错误。

有关更多信息,请参阅sgcheck文档:

http://valgrind.org/docs/manual/sg-manual.html

添加日志:

valgrind --tool=exp-sgcheck ./a.out