Fix valgrind errors with memmove and talloc pools.
authorJeremy Allison <jra@samba.org>
Tue, 27 Aug 2013 20:20:43 +0000 (13:20 -0700)
committerStefan Metzmacher <metze@samba.org>
Tue, 10 Mar 2015 09:55:37 +0000 (10:55 +0100)
bin/smbtorture //127.0.0.1 local.talloc now runs with no valgrind errors.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: "Stefan (metze) Metzmacher" <metze@samba.org>
Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Wed Aug 28 02:44:17 CEST 2013 on sn-devel-104

(cherry picked from commit 617c647b8ef562ace589a11a15eb460e6db71f2a)

lib/talloc/talloc.c

index 677ec0f13fda4884f7da3340cf3b74d10603ad6a..69d5a16c0a1665b71f5f8667815540c579fabf88 100644 (file)
@@ -1609,6 +1609,27 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
                                size_t old_used = TC_HDR_SIZE + tc->size;
                                size_t new_used = TC_HDR_SIZE + size;
                                new_ptr = start;
+
+#if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
+                               {
+                                       /*
+                                        * The area from
+                                        * start -> tc may have
+                                        * been freed and thus been marked as
+                                        * VALGRIND_MEM_NOACCESS. Set it to
+                                        * VALGRIND_MEM_UNDEFINED so we can
+                                        * copy into it without valgrind errors.
+                                        * We can't just mark
+                                        * new_ptr -> new_ptr + old_used
+                                        * as this may overlap on top of tc,
+                                        * (which is why we use memmove, not
+                                        * memcpy below) hence the MIN.
+                                        */
+                                       size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
+                                       VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
+                               }
+#endif
+
                                memmove(new_ptr, tc, old_used);
 
                                tc = (struct talloc_chunk *)new_ptr;