From eca62c130353147121431b5893f11246de001269 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Mon, 27 Dec 2021 09:55:59 +0300 Subject: Eliminate 'writing into region of size 0' gcc FP warning in realloc Issue #406 (bdwgc). * mallocx.c [_FORTIFY_SOURCE && GC_GNUC_PREREQ(9,0) && !__clang__] (GC_realloc): Declare cleared_p local variable as volatile; move and update comment. * mallocx.c (GC_realloc): Declare cleared_p local variable at the top level; * mallocx.c [!IGNORE_FREE] (GC_realloc): Pass cleared_p to GC_free() unless lb is 0. --- mallocx.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'mallocx.c') diff --git a/mallocx.c b/mallocx.c index 4e65fa09..0692565e 100644 --- a/mallocx.c +++ b/mallocx.c @@ -79,6 +79,12 @@ GC_API void * GC_CALL GC_realloc(void * p, size_t lb) struct hblk * h; hdr * hhdr; void * result; +# if defined(_FORTIFY_SOURCE) && GC_GNUC_PREREQ(9, 0) && !defined(__clang__) + volatile /* Use cleared_p instead of p as a workaround to avoid */ + /* passing alloc_size(lb) attribute associated with p */ + /* to memset (including memset call inside GC_free). */ +# endif + word cleared_p = (word)p; size_t sz; /* Current size in bytes */ size_t orig_sz; /* Original sz in bytes */ int obj_kind; @@ -146,10 +152,6 @@ GC_API void * GC_CALL GC_realloc(void * p, size_t lb) if (orig_sz > lb) { /* Clear unneeded part of object to avoid bogus pointer */ /* tracing. */ - word cleared_p = (word)p; - /* A workaround to avoid passing alloc_size(lb) */ - /* attribute associated with p to memset. */ - BZERO((ptr_t)cleared_p + lb, orig_sz - lb); } return(p); @@ -163,7 +165,7 @@ GC_API void * GC_CALL GC_realloc(void * p, size_t lb) /* But this gives the client warning of imminent disaster. */ BCOPY(p, result, sz); # ifndef IGNORE_FREE - GC_free(p); + GC_free((ptr_t)cleared_p); # endif } return result; -- cgit v1.2.1