From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12723 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH 1/2] malloc: fix an over-allocation bug Date: Mon, 16 Apr 2018 18:40:42 -0400 Message-ID: <20180416224042.GB3094@brightrain.aerifal.cx> References: <20180416175436.2384-1-amonakov@ispras.ru> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1523918332 23991 195.159.176.226 (16 Apr 2018 22:38:52 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 16 Apr 2018 22:38:52 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-12739-gllmg-musl=m.gmane.org@lists.openwall.com Tue Apr 17 00:38:48 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 1f8Clr-0006Ai-2R for gllmg-musl@m.gmane.org; Tue, 17 Apr 2018 00:38:47 +0200 Original-Received: (qmail 11340 invoked by uid 550); 16 Apr 2018 22:40:55 -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 11297 invoked from network); 16 Apr 2018 22:40:54 -0000 Content-Disposition: inline In-Reply-To: <20180416175436.2384-1-amonakov@ispras.ru> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:12723 Archived-At: On Mon, Apr 16, 2018 at 08:54:35PM +0300, Alexander Monakov wrote: > 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; Looks ok. I've got this queued as submitted. Rich