From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gCJP8-0000JY-Dp for barebox@lists.infradead.org; Tue, 16 Oct 2018 07:04:36 +0000 Date: Tue, 16 Oct 2018 09:04:22 +0200 From: Sascha Hauer Message-ID: <20181016070422.kgfyd5liphufekjo@pengutronix.de> References: <20181015170021.22617-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20181015170021.22617-1-andrew.smirnov@gmail.com> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: Re: [PATCH 1/5] tlsf_malloc: dummy_malloc: Share code for calloc() To: Andrey Smirnov Cc: barebox@lists.infradead.org On Mon, Oct 15, 2018 at 10:00:17AM -0700, Andrey Smirnov wrote: > Calloc() implementation for TLSF does not correctly check for malloc() > failure which can result in a NULL pointer exception when trying to > calloc() extra large buffers. > > Since both TLSF and dummy malloc implementations of calloc() are > exactly the same, pick implementation for the latter (which does > aforementioned check) and share it between the two. > Applied all, thanks Sascha > Signed-off-by: Andrey Smirnov > --- > common/Makefile | 4 ++-- > common/calloc.c | 19 +++++++++++++++++++ > common/dummy_malloc.c | 13 ------------- > common/tlsf_malloc.c | 16 ---------------- > 4 files changed, 21 insertions(+), 31 deletions(-) > create mode 100644 common/calloc.c > > diff --git a/common/Makefile b/common/Makefile > index 13920cc5a..861365bd5 100644 > --- a/common/Makefile > +++ b/common/Makefile > @@ -34,8 +34,8 @@ obj-$(CONFIG_GLOBALVAR) += globalvar.o > obj-$(CONFIG_GREGORIAN_CALENDER) += date.o > obj-$(CONFIG_KALLSYMS) += kallsyms.o > obj-$(CONFIG_MALLOC_DLMALLOC) += dlmalloc.o > -obj-$(CONFIG_MALLOC_TLSF) += tlsf_malloc.o tlsf.o > -obj-$(CONFIG_MALLOC_DUMMY) += dummy_malloc.o > +obj-$(CONFIG_MALLOC_TLSF) += tlsf_malloc.o tlsf.o calloc.o > +obj-$(CONFIG_MALLOC_DUMMY) += dummy_malloc.o calloc.o > obj-$(CONFIG_MEMINFO) += meminfo.o > obj-$(CONFIG_MENU) += menu.o > obj-$(CONFIG_MODULES) += module.o > diff --git a/common/calloc.c b/common/calloc.c > new file mode 100644 > index 000000000..2b933ec27 > --- /dev/null > +++ b/common/calloc.c > @@ -0,0 +1,19 @@ > +#include > +#include > + > +/* > + * calloc calls malloc, then zeroes out the allocated chunk. > + */ > +void *calloc(size_t n, size_t elem_size) > +{ > + size_t size = elem_size * n; > + void *r = malloc(size); > + > + if (!r) > + return r; > + > + memset(r, 0x0, size); > + > + return r; > +} > +EXPORT_SYMBOL(calloc); > diff --git a/common/dummy_malloc.c b/common/dummy_malloc.c > index 641baa125..fa4f5d126 100644 > --- a/common/dummy_malloc.c > +++ b/common/dummy_malloc.c > @@ -50,16 +50,3 @@ void *realloc(void *ptr, size_t size) > { > BUG(); > } > - > -void *calloc(size_t n, size_t elem_size) > -{ > - size_t size = elem_size * n; > - void *r = malloc(size); > - > - if (!r) > - return r; > - > - memset(r, 0x0, size); > - > - return r; > -} > diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c > index a3541d825..aa3ab2397 100644 > --- a/common/tlsf_malloc.c > +++ b/common/tlsf_malloc.c > @@ -39,22 +39,6 @@ void *malloc(size_t bytes) > } > EXPORT_SYMBOL(malloc); > > -/* > - * calloc calls malloc, then zeroes out the allocated chunk. > - */ > -void *calloc(size_t n, size_t elem_size) > -{ > - void *mem; > - size_t sz; > - > - sz = n * elem_size; > - mem = malloc(sz); > - memset(mem, 0, sz); > - > - return mem; > -} > -EXPORT_SYMBOL(calloc); > - > void free(void *mem) > { > tlsf_free(tlsf_mem_pool, mem); > -- > 2.17.1 > > > _______________________________________________ > barebox mailing list > barebox@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/barebox > -- Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox