Valgrind (または具体的にはMemcheck )を使用してあらゆる種類の動的メモリ関連のエラーをテストする過程で、意図的にメモリのオーバーラップを作成している状況に遭遇しましたが、Valgrind/memcheck からのエラー レポートはありません。 . 以下は使用したコードです。私が欠けているものを共有してください。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char * pOne;
char * pTwo;
pOne = (char *)malloc (24);
pTwo = pOne + 4;
strcpy (pOne, "Sourav Ghosh");
printf("pOne = %s\npTwo = %s\n", pOne, pTwo);
memcpy (pTwo, pOne, 16); //Overlapping issue should be here
printf("pOne = %s\npTwo = %s\n", pOne, pTwo);
free (pOne);
return 0;
}
コンパイル
[sourav@titan temp]$ gcc -g srvtest.c -o memory
サンプルの実行と出力
[sourav@titan temp]$ valgrind --leak-check=full ./memory
==6982== Memcheck, a memory error detector
==6982== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==6982== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==6982== Command: ./memory
==6982==
pOne = Sourav Ghosh
pTwo = av Ghosh
==6982== Conditional jump or move depends on uninitialised value(s)
==6982== at 0x4006817: strlen (mc_replace_strmem.c:275)
==6982== by 0xAD0C0D: vfprintf (in /lib/libc-2.5.so)
==6982== by 0xAD6E82: printf (in /lib/libc-2.5.so)
==6982== by 0x8048477: main (srvtest.c:15)
==6982==
pOne = SourSourSourSourSour
pTwo = SourSourSourSour
==6982==
==6982== HEAP SUMMARY:
==6982== in use at exit: 0 bytes in 0 blocks
==6982== total heap usage: 1 allocs, 1 frees, 24 bytes allocated
==6982==
==6982== All heap blocks were freed -- no leaks are possible
==6982==
==6982== For counts of detected and suppressed errors, rerun with: -v
==6982== Use --track-origins=yes to see where uninitialised values come from
==6982== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 12 from 8)
[sourav@titan temp]$
メモリ領域のオーバーラップに関する情報は実際にはありません。Valgrind のマニュアルによると、次のように表示されているはずです。
==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
私の場合、欠けている部分は何ですか?
システムインフォメーション:
[sourav@titan temp]$ uname -r
2.6.18-194.el5PAE
[sourav@titan temp]$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
[sourav@titan temp]$ ldd --version
ldd (GNU libc) 2.5
[sourav@titan temp]$ valgrind --version
valgrind-3.5.0