From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-4.0 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 1038 invoked from network); 2 Aug 2020 08:07:33 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 2 Aug 2020 08:07:33 -0000 Received: (qmail 13346 invoked by uid 550); 2 Aug 2020 08:07:28 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 13328 invoked from network); 2 Aug 2020 08:07:28 -0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=samersoff.net; s=x; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:To:Subject:Sender:Reply-To:Cc: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=hPMgYmOnDEIoRp2NeK5uxBV9GLv0oeRimCiRMEZ69n4=; b=VDgfXjjkoaC8Z1u2RKsxs0Nt5n 9+/vTGxhto8fCq/9bnGRSaZMFVc6u8usP+sXGZToYyFaQHoz4Z71m71gUB+Tp96HDgCOVI/Rz25md SL3AdsWwJ4MI2NZlOAh7uCy99mEOcpy7mvIc46Xg1BUy/Tjs/HG6jC0KYwofqmW3o1J0=; To: musl@lists.openwall.com, Ariadne Conill References: <20200801214216.10452-1-ariadne@dereferenced.org> From: Dmitry Samersoff Message-ID: <6718676e-9067-6a46-d25f-80c508e3976a@samersoff.net> Date: Sun, 2 Aug 2020 11:07:16 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <20200801214216.10452-1-ariadne@dereferenced.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-As: dms X-SpamProbe: GOOD 0.0000070 f587198d26a78979f527065b84dc9232 Subject: Re: [musl] [PATCH v3] implement recallocarray(3) Ariadne, BSD (jemalloc) realloc always perform a new allocation and may return completely new pointer ever if new_size <= old_size. Also contract for realloc says that if allocation fails original content should remain untouched. I don't know how musl malloc perform in this case, but it might be better to move all memset after realloc and use newptr as a memeset base, with an appropriate error checking. -Dmitry On 02.08.2020 0:42, Ariadne Conill wrote: > This OpenBSD extension is similar to reallocarray(3), but > zero-initializes the new memory area. > > This extension is placed in _BSD_SOURCE, like > reallocarray(3). > > Changes from v2: > - drop overflow checking for old size > > Changes from v1: > - use realloc() instead of reallocarray() > --- > include/stdlib.h | 1 + > src/malloc/recallocarray.c | 27 +++++++++++++++++++++++++++ > 2 files changed, 28 insertions(+) > create mode 100644 src/malloc/recallocarray.c > > diff --git a/include/stdlib.h b/include/stdlib.h > index b54a051f..a0412ad4 100644 > --- a/include/stdlib.h > +++ b/include/stdlib.h > @@ -146,6 +146,7 @@ int clearenv(void); > #define WCOREDUMP(s) ((s) & 0x80) > #define WIFCONTINUED(s) ((s) == 0xffff) > void *reallocarray (void *, size_t, size_t); > +void *recallocarray (void *, size_t, size_t, size_t); > #endif > > #ifdef _GNU_SOURCE > diff --git a/src/malloc/recallocarray.c b/src/malloc/recallocarray.c > new file mode 100644 > index 00000000..a7827604 > --- /dev/null > +++ b/src/malloc/recallocarray.c > @@ -0,0 +1,27 @@ > +#define _BSD_SOURCE > +#include > +#include > +#include > + > +void *recallocarray(void *ptr, size_t om, size_t m, size_t n) > +{ > + void *newptr; > + size_t old_size = om * n, new_size; > + > + if (n && m > -1 / n) { > + errno = ENOMEM; > + return 0; > + } > + new_size = m * n; > + > + if (new_size <= old_size) { > + memset((char *) ptr + new_size, 0, old_size - new_size); > + } > + > + newptr = realloc(ptr, m * n); > + if (new_size > old_size) { > + memset((char *) ptr + old_size, 0, new_size - old_size); > + } > + > + return newptr; > +} >