]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
memcheck: Handle Err_ReallocSizeZero in MC_(eq_Error)
authorMark Wielaard <mark@klomp.org>
Thu, 1 Jun 2023 14:10:56 +0000 (16:10 +0200)
committerMark Wielaard <mark@klomp.org>
Fri, 2 Jun 2023 10:05:03 +0000 (12:05 +0200)
When an realloc size zero error is emitted MC_(eq_Error) is called to
see if the errors can be deduplicated. This crashed since
Err_ReallocSizeZero wasn't handled. Handle it like Err_Free.

Also add a testcase for this case and test with both
--realloc-zero-bytes-frees=yes and
--realloc-zero-bytes-frees=no.
Which will report a different number of errors.

https://bugs.kde.org/show_bug.cgi?id=470520

.gitignore
NEWS
memcheck/mc_errors.c
memcheck/tests/Makefile.am
memcheck/tests/realloc_size_zero_again.c [new file with mode: 0644]
memcheck/tests/realloc_size_zero_again_no.stderr.exp [new file with mode: 0644]
memcheck/tests/realloc_size_zero_again_no.stdout.exp [new file with mode: 0644]
memcheck/tests/realloc_size_zero_again_no.vgtest [new file with mode: 0644]
memcheck/tests/realloc_size_zero_again_yes.stderr.exp [new file with mode: 0644]
memcheck/tests/realloc_size_zero_again_yes.stdout.exp [new file with mode: 0644]
memcheck/tests/realloc_size_zero_again_yes.vgtest [new file with mode: 0644]

index 076e168deda4e1e092be74f56bbbd5357988f27f..6d73324cea4f03ee956bdf8da7bb9301dcd166e0 100644 (file)
 /memcheck/tests/post-syscall
 /memcheck/tests/reach_thread_register
 /memcheck/tests/realloc_size_zero
+/memcheck/tests/realloc_size_zero_again
 /memcheck/tests/realloc_size_zero_mismatch
 /memcheck/tests/realloc1
 /memcheck/tests/realloc2
diff --git a/NEWS b/NEWS
index ea9fc7c868337a42d33ecc3a0014221c80134b54..09f8c713704afecd164b316363abac8c11002b94 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,7 @@ are not entered into bugzilla tend to get forgotten about or ignored.
 469049  link failure on ppc64 (big endian) valgrind 3.20
 469146  massif --ignore-fn does not ignore inlined functions
 469768  Make it possible to install gdb scripts in a different location
+470520  Multiple realloc zero errors crash in MC_(eq_Error)
 
 To see details of a given bug, visit
   https://bugs.kde.org/show_bug.cgi?id=XXXXXX
index 00d6ec301eb1ee4cff2150cf2553aa4caf77e941..65210a2209b3825dec636a3914b237c62adb29f7 100644 (file)
@@ -1041,6 +1041,7 @@ Bool MC_(eq_Error) ( VgRes res, const Error* e1, const Error* e2 )
       case Err_IllegalMempool:
       case Err_Overlap:
       case Err_Cond:
+      case Err_ReallocSizeZero:
          return True;
 
       case Err_FishyValue:
index 71c38acbafbb8f7102737a70b16ead3605a8f2e5..5a17fd35d49a73a269ebc5ee2e72b0fbdb707652 100644 (file)
@@ -291,8 +291,14 @@ EXTRA_DIST = \
                realloc_size_zero.vgtest \
        realloc_size_zero_yes.stderr.exp realloc_size_zero_yes.stdout.exp \
                realloc_size_zero_yes.vgtest \
+       realloc_size_zero_again_yes.stderr.exp \
+               realloc_size_zero_again_yes.stdout.exp \
+               realloc_size_zero_again_yes.vgtest \
        realloc_size_zero_no.stderr.exp realloc_size_zero_no.stdout.exp \
                realloc_size_zero_no.vgtest \
+       realloc_size_zero_again_no.stderr.exp \
+               realloc_size_zero_again_no.stdout.exp \
+               realloc_size_zero_again_no.vgtest \
        realloc_size_zero_off.stderr.exp realloc_size_zero_off.stdout.exp \
                realloc_size_zero_off.vgtest \
        realloc_size_zero_mismatch.stderr.exp \
@@ -459,6 +465,7 @@ check_PROGRAMS = \
        posix_memalign \
        post-syscall \
        realloc_size_zero realloc_size_zero_mismatch \
+       realloc_size_zero_again \
        realloc1 realloc2 realloc3 \
        recursive-merge \
        resvn_stack \
diff --git a/memcheck/tests/realloc_size_zero_again.c b/memcheck/tests/realloc_size_zero_again.c
new file mode 100644 (file)
index 0000000..782d4bd
--- /dev/null
@@ -0,0 +1,15 @@
+#include <stdlib.h>
+
+int
+main ()
+{
+  char *p = malloc (1024);
+  for (int i = 3; i >= 0; i--)
+    for (int j = 0; j <= 3; j++)
+      {
+       char *q = realloc (p, i * j * 512);
+       p = q;
+      }
+
+  free (p);
+}
diff --git a/memcheck/tests/realloc_size_zero_again_no.stderr.exp b/memcheck/tests/realloc_size_zero_again_no.stderr.exp
new file mode 100644 (file)
index 0000000..b9c061d
--- /dev/null
@@ -0,0 +1,18 @@
+realloc() with size 0
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   ...
+ Address 0x........ is 0 bytes inside a block of size 1,024 alloc'd
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   ...
+
+ERROR SUMMARY: 7 errors from 1 contexts (suppressed: 0 from 0)
+
+7 errors in context 1 of 1:
+realloc() with size 0
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   ...
+ Address 0x........ is 0 bytes inside a block of size 1,024 alloc'd
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   ...
+
+ERROR SUMMARY: 7 errors from 1 contexts (suppressed: 0 from 0)
diff --git a/memcheck/tests/realloc_size_zero_again_no.stdout.exp b/memcheck/tests/realloc_size_zero_again_no.stdout.exp
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/memcheck/tests/realloc_size_zero_again_no.vgtest b/memcheck/tests/realloc_size_zero_again_no.vgtest
new file mode 100644 (file)
index 0000000..f1757b6
--- /dev/null
@@ -0,0 +1,2 @@
+prog: realloc_size_zero_again
+vgopts: -q -s --realloc-zero-bytes-frees=no
diff --git a/memcheck/tests/realloc_size_zero_again_yes.stderr.exp b/memcheck/tests/realloc_size_zero_again_yes.stderr.exp
new file mode 100644 (file)
index 0000000..d40aa24
--- /dev/null
@@ -0,0 +1,18 @@
+realloc() with size 0
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   ...
+ Address 0x........ is 0 bytes inside a block of size 1,024 alloc'd
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   ...
+
+ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0)
+
+5 errors in context 1 of 1:
+realloc() with size 0
+   at 0x........: realloc (vg_replace_malloc.c:...)
+   ...
+ Address 0x........ is 0 bytes inside a block of size 1,024 alloc'd
+   at 0x........: malloc (vg_replace_malloc.c:...)
+   ...
+
+ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0)
diff --git a/memcheck/tests/realloc_size_zero_again_yes.stdout.exp b/memcheck/tests/realloc_size_zero_again_yes.stdout.exp
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/memcheck/tests/realloc_size_zero_again_yes.vgtest b/memcheck/tests/realloc_size_zero_again_yes.vgtest
new file mode 100644 (file)
index 0000000..215392e
--- /dev/null
@@ -0,0 +1,2 @@
+prog: realloc_size_zero_again
+vgopts: -q -s --realloc-zero-bytes-frees=yes