From mboxrd@z Thu Jan 1 00:00:00 1970 From: Oleksandr Natalenko Subject: [PATCH v2 1/2] utils: provide array allocation wrapper Date: Mon, 2 Jan 2017 20:54:08 +0100 Message-ID: <20170102195409.14669-2-oleksandr@natalenko.name> References: <20170102195409.14669-1-oleksandr@natalenko.name> To: netfilter-devel@vger.kernel.org Return-path: Received: from vulcan.natalenko.name ([104.207.131.136]:44558 "EHLO vulcan.natalenko.name" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933349AbdABTyS (ORCPT ); Mon, 2 Jan 2017 14:54:18 -0500 Received: from localhost.localdomain (unknown [IPv6:2001:470:5b39:29:c99:116f:61fe:9756]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by vulcan.natalenko.name (Postfix) with ESMTPSA id 75F6F16AAEB for ; Mon, 2 Jan 2017 20:54:15 +0100 (CET) In-Reply-To: <20170102195409.14669-1-oleksandr@natalenko.name> Sender: netfilter-devel-owner@vger.kernel.org List-ID: This will be used for allocating memory for arrays in heap instead of keeping them on stack. Signed-off-by: Oleksandr Natalenko --- include/utils.h | 1 + src/utils.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/utils.h b/include/utils.h index bb58ba4..3199388 100644 --- a/include/utils.h +++ b/include/utils.h @@ -138,6 +138,7 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor extern void xfree(const void *ptr); extern void *xmalloc(size_t size); +extern void *xmalloc_array(size_t nmemb, size_t size); extern void *xrealloc(void *ptr, size_t size); extern void *xzalloc(size_t size); extern char *xstrdup(const char *s); diff --git a/src/utils.c b/src/utils.c index 65dabf4..16bc9e2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -39,6 +39,16 @@ void *xmalloc(size_t size) return ptr; } +void *xmalloc_array(size_t nmemb, size_t size) +{ + assert(size != 0); + + if (nmemb > SIZE_MAX / size) + memory_allocation_error(); + + return xmalloc(nmemb * size); +} + void *xrealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); -- 2.11.0