From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12719 Path: news.gmane.org!.POSTED!not-for-mail From: Alexander Monakov Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 1/2] malloc: fix an over-allocation bug Date: Mon, 16 Apr 2018 20:54:35 +0300 Message-ID: <20180416175436.2384-1-amonakov@ispras.ru> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1523901199 3333 195.159.176.226 (16 Apr 2018 17:53:19 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 16 Apr 2018 17:53:19 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-12735-gllmg-musl=m.gmane.org@lists.openwall.com Mon Apr 16 19:53:15 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1f88JU-0000lJ-Ep for gllmg-musl@m.gmane.org; Mon, 16 Apr 2018 19:53:12 +0200 Original-Received: (qmail 23839 invoked by uid 550); 16 Apr 2018 17:55:15 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 23780 invoked from network); 16 Apr 2018 17:55:14 -0000 X-Mailer: git-send-email 2.11.0 Xref: news.gmane.org gmane.linux.lib.musl.general:12719 Archived-At: Fix an instance where realloc code would overallocate by OVERHEAD bytes amount. Manually arrange for reuse of memcpy-free-return exit sequence. --- src/malloc/malloc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c index 9e05e1d6..1af4ae5a 100644 --- a/src/malloc/malloc.c +++ b/src/malloc/malloc.c @@ -397,10 +397,9 @@ void *realloc(void *p, size_t n) size_t newlen = n + extra; /* Crash on realloc of freed chunk */ if (extra & 1) a_crash(); - if (newlen < PAGE_SIZE && (new = malloc(n))) { - memcpy(new, p, n-OVERHEAD); - free(p); - return new; + if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) { + n0 = n; + goto copy_free_ret; } newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; if (oldlen == newlen) return p; @@ -443,6 +442,7 @@ copy_realloc: /* As a last resort, allocate a new chunk and copy to it. */ new = malloc(n-OVERHEAD); if (!new) return 0; +copy_free_ret: memcpy(new, p, n0-OVERHEAD); free(CHUNK_TO_MEM(self)); return new; -- 2.11.0