From: Dirk Mueller Date: Wed, 22 Feb 2006 13:23:33 +0000 (+0000) Subject: backport "calloc does not always zero memory" (v5647) X-Git-Tag: svn/VALGRIND_3_1_1~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9240a8c1d0befe7f004232326079035a3e140c8c;p=thirdparty%2Fvalgrind.git backport "calloc does not always zero memory" (v5647) git-svn-id: svn://svn.valgrind.org/valgrind/branches/VALGRIND_3_1_BRANCH@5678 --- diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c index ffb1debfaf..d1714e19cb 100644 --- a/coregrind/m_syswrap/syswrap-generic.c +++ b/coregrind/m_syswrap/syswrap-generic.c @@ -947,6 +947,23 @@ static Addr do_brk ( Addr newbrk ) if (seg && seg->hasT) VG_(discard_translations)( newbrk, VG_(brk_limit) - newbrk, "do_brk(shrink)" ); + /* Since we're being lazy and not unmapping pages, we have to + zero out the area, so that if the area later comes back into + circulation, it will be filled with zeroes, as if it really + had been unmapped and later remapped. Be a bit paranoid and + try hard to ensure we're not going to segfault by doing the + write - check both ends of the range are in the same segment + and that segment is writable. */ + if (seg) { + /* pre: newbrk < VG_(brk_limit) + => newbrk <= VG_(brk_limit)-1 */ + NSegment* seg2; + vg_assert(newbrk < VG_(brk_limit)); + seg2 = VG_(am_find_nsegment)( VG_(brk_limit)-1 ); + if (seg2 && seg == seg2 && seg->hasW) + VG_(memset)( (void*)newbrk, 0, VG_(brk_limit) - newbrk ); + } + VG_(brk_limit) = newbrk; return newbrk; }