]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Changed the behaviour of realloc() in Memcheck, Addrcheck and Helgrind.
authorNicholas Nethercote <njn@valgrind.org>
Thu, 24 Jul 2003 17:39:59 +0000 (17:39 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Thu, 24 Jul 2003 17:39:59 +0000 (17:39 +0000)
Previously, when realloc() was asked to make a block bigger, the ExeContext
describing where that block was allocated was increased;  however, if the block
became smaller or stayed the same size, the original ExeContext remained.  This
is correct in one way (that's where the memory manager actually parcelled out
the block) but it's not very intuitive.  This commit changes things so the
ExeContext of a block is always changed upon realloc().  I added a regression
test for it too.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1783

helgrind/hg_main.c
memcheck/mac_malloc_wrappers.c
memcheck/tests/Makefile.am
memcheck/tests/realloc3.c [new file with mode: 0644]
memcheck/tests/realloc3.stderr.exp [new file with mode: 0644]
memcheck/tests/realloc3.vgtest [new file with mode: 0644]

index d95173e4144bf79a6bf5568959ced94da9629143..9a03ca402f08b3253234db4be572d2961f5289af 100644 (file)
@@ -1965,11 +1965,13 @@ void* SK_(realloc) ( void* p, Int new_size )
   
    if (hc->size == new_size) {
       /* size unchanged */
+      hc->where = VG_(get_ExeContext)(tid);
       return p;
       
    } else if (hc->size > new_size) {
       /* new size is smaller */
       hc->size = new_size;
+      hc->where = VG_(get_ExeContext)(tid);
       return p;
 
    } else {
index 879bffd3b1481ca57b9efe472eca4ff3d58d9a41..536cc2e122ed1f273a7a1d24f9180c0c970309af 100644 (file)
@@ -354,6 +354,7 @@ void* SK_(realloc) ( void* p, Int new_size )
 
    if (mc->size == new_size) {
       /* size unchanged */
+      mc->where = VG_(get_ExeContext)(tid);
       VGP_POPCC(VgpCliMalloc);
       return p;
       
@@ -361,6 +362,7 @@ void* SK_(realloc) ( void* p, Int new_size )
       /* new size is smaller */
       MAC_(die_mem_heap)( mc->data+new_size, mc->size-new_size );
       mc->size = new_size;
+      mc->where = VG_(get_ExeContext)(tid);
       VGP_POPCC(VgpCliMalloc);
       return p;
 
index 86b20a3988cde7598a798ed3e60920264ca1bcd2..4f9dc16aa1ba0d8f43d7d9df7b577b12b4cb278e 100644 (file)
@@ -44,6 +44,7 @@ EXTRA_DIST = $(noinst_SCRIPTS) \
        pushfpopf.stderr.exp pushfpopf.stdout.exp pushfpopf.vgtest \
        realloc1.stderr.exp realloc1.vgtest \
        realloc2.stderr.exp realloc2.vgtest \
+       realloc3.stderr.exp realloc3.vgtest \
        sigaltstack.stderr.exp sigaltstack.vgtest \
        signal2.stderr.exp \
        signal2.stdout.exp signal2.vgtest \
@@ -64,7 +65,7 @@ check_PROGRAMS = \
        malloc1 malloc2 malloc3 manuel1 manuel2 manuel3 \
        memalign_test memcmptest mmaptest nanoleak null_socket \
        overlap pushfpopf \
-       realloc1 realloc2 sigaltstack signal2 supp1 supp2 suppfree \
+       realloc1 realloc2 realloc3 sigaltstack signal2 supp1 supp2 suppfree \
        trivialleak tronical weirdioctl \
        mismatches new_override metadata threadederrno
 
@@ -103,6 +104,7 @@ overlap_SOURCES     = overlap.c
 pushfpopf_SOURCES      = pushfpopf_c.c pushfpopf_s.s
 realloc1_SOURCES       = realloc1.c
 realloc2_SOURCES       = realloc2.c
+realloc3_SOURCES       = realloc3.c
 signal2_SOURCES        = signal2.c
 supp1_SOURCES          = supp.c
 supp2_SOURCES          = supp.c
diff --git a/memcheck/tests/realloc3.c b/memcheck/tests/realloc3.c
new file mode 100644 (file)
index 0000000..0c6a5f8
--- /dev/null
@@ -0,0 +1,28 @@
+/* For a long time (from Valgrind 1.0 to 1.9.6, AFAICT) when realloc() was
+   called and made a block smaller, or didn't change its size, the
+   ExeContext of the block was not updated;  therefore any errors that
+   referred to it would state that it was allocated not by the realloc(),
+   but by the previous malloc() or whatever.  While this is true in one
+   sense, it is misleading and not what you'd expect.  This test
+   demonstrates this -- 'x' and 'y' are unchanged and shrunk, and their
+   ExeContexts should be updated upon their realloc().  I hope that's clear.
+*/
+#include <stdlib.h>
+
+int main(void)
+{
+   int* x = malloc(5);
+   int* y = malloc(10);
+   int* z = malloc(2);
+   int a, b, c;
+
+   x = realloc(x, 5);   // same size
+   y = realloc(y, 5);   // make smaller
+   z = realloc(z, 5);   // make bigger
+
+   a = (x[5] == 0xdeadbeef ? 1 : 0);
+   b = (y[5] == 0xdeadbeef ? 1 : 0);
+   c = (z[5] == 0xdeadbeef ? 1 : 0);
+
+   return a + b + c;
+}
diff --git a/memcheck/tests/realloc3.stderr.exp b/memcheck/tests/realloc3.stderr.exp
new file mode 100644 (file)
index 0000000..6704188
--- /dev/null
@@ -0,0 +1,29 @@
+Invalid read of size 4
+   at 0x........: main (realloc3.c:23)
+   by 0x........: __libc_start_main (...libc...)
+   by 0x........: ...
+   Address 0x........ is 15 bytes after a block of size 5 alloc'd
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   by 0x........: main (realloc3.c:19)
+   by 0x........: __libc_start_main (...libc...)
+   by 0x........: ...
+
+Invalid read of size 4
+   at 0x........: main (realloc3.c:24)
+   by 0x........: __libc_start_main (...libc...)
+   by 0x........: ...
+   Address 0x........ is 15 bytes after a block of size 5 alloc'd
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   by 0x........: main (realloc3.c:20)
+   by 0x........: __libc_start_main (...libc...)
+   by 0x........: ...
+
+Invalid read of size 4
+   at 0x........: main (realloc3.c:25)
+   by 0x........: __libc_start_main (...libc...)
+   by 0x........: ...
+   Address 0x........ is 15 bytes after a block of size 5 alloc'd
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   by 0x........: main (realloc3.c:21)
+   by 0x........: __libc_start_main (...libc...)
+   by 0x........: ...
diff --git a/memcheck/tests/realloc3.vgtest b/memcheck/tests/realloc3.vgtest
new file mode 100644 (file)
index 0000000..ff295ab
--- /dev/null
@@ -0,0 +1,2 @@
+prog: realloc3
+vgopts: -q