From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from elaine.keithp.com (home.keithp.com [63.227.221.253]) by sourceware.org (Postfix) with ESMTPS id 67D6E3857C54 for ; Fri, 14 Aug 2020 00:19:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 67D6E3857C54 Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id 3AB1A3F2D43D for ; Thu, 13 Aug 2020 17:19:08 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at keithp.com Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id wRkc6Ibpz5XK; Thu, 13 Aug 2020 17:19:08 -0700 (PDT) Received: from keithp.com (koto.keithp.com [10.0.0.2]) by elaine.keithp.com (Postfix) with ESMTPSA id 3D4203F2D40C; Thu, 13 Aug 2020 17:19:07 -0700 (PDT) Received: by keithp.com (Postfix, from userid 1000) id 1C40D15821C0; Thu, 13 Aug 2020 17:19:07 -0700 (PDT) From: Keith Packard To: newlib@sourceware.org Subject: [PATCH 1/2] libm/stdlib: don't read past source in nano_realloc Date: Thu, 13 Aug 2020 17:19:01 -0700 Message-Id: <20200814001902.510489-2-keithp@keithp.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814001902.510489-1-keithp@keithp.com> References: <20200814001902.510489-1-keithp@keithp.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Aug 2020 00:19:10 -0000 Save the computed block size and use it to avoid reading past the end of the source block. Signed-off-by: Keith Packard --- newlib/libc/stdlib/nano-mallocr.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c index 04465eb9e..5028431ec 100644 --- a/newlib/libc/stdlib/nano-mallocr.c +++ b/newlib/libc/stdlib/nano-mallocr.c @@ -466,6 +466,7 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size) { void * mem; chunk * p_to_realloc; + malloc_size_t old_size; if (ptr == NULL) return nano_malloc(RCALL size); @@ -477,13 +478,14 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size) /* TODO: There is chance to shrink the chunk if newly requested * size is much small */ - if (nano_malloc_usable_size(RCALL ptr) >= size) + old_size = nano_malloc_usable_size(RCALL ptr); + if (old_size >= size) return ptr; mem = nano_malloc(RCALL size); if (mem != NULL) { - memcpy(mem, ptr, size); + memcpy(mem, ptr, old_size); nano_free(RCALL ptr); } return mem; -- 2.28.0