From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 09C831F4B4 for ; Mon, 19 Apr 2021 04:04:31 +0000 (UTC) Received: from localhost ([::1]:44354 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lYL9C-0001WD-6W for normalperson@yhbt.net; Mon, 19 Apr 2021 00:04:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51210) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lYL7S-00084U-60 for bug-gnulib@gnu.org; Mon, 19 Apr 2021 00:02:43 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:46316) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lYL7N-0004Km-Sj for bug-gnulib@gnu.org; Mon, 19 Apr 2021 00:02:41 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id D8D1C16012C; Sun, 18 Apr 2021 21:02:36 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id oH-SE0rRRP38; Sun, 18 Apr 2021 21:02:35 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id D36BE16011A; Sun, 18 Apr 2021 21:02:35 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id aCSRS50jQ7GE; Sun, 18 Apr 2021 21:02:35 -0700 (PDT) Received: from day.example.com (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id AC23A160095; Sun, 18 Apr 2021 21:02:35 -0700 (PDT) From: Paul Eggert To: bug-gnulib@gnu.org, berrange@redhat.com Subject: [PATCH 4/8] xalloc: new function xreallocarray Date: Sun, 18 Apr 2021 21:01:55 -0700 Message-Id: <20210419040158.1902066-4-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210419040158.1902066-1-eggert@cs.ucla.edu> References: <20210419040158.1902066-1-eggert@cs.ucla.edu> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=131.179.128.68; envelope-from=eggert@cs.ucla.edu; helo=zimbra.cs.ucla.edu X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paul Eggert Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" This effectively replaces xnmalloc, which perhaps should be deprecated. The name xreallocarray should be easier to remember now that reallocarray is a standard GNU function. * lib/xalloc.h [GNULIB_XALLOC]: Do not include xalloc-oversized.h. (xnmalloc, xnrealloc, x2nrealloc): Simplify by using xreallocarray. * lib/xmalloc.c (xreallocarray): New function. * modules/xalloc (Depends-on): Add reallocarray; remove xalloc-oversized. --- ChangeLog | 10 ++++++++++ lib/xalloc.h | 26 ++++++++++++++------------ lib/xmalloc.c | 12 ++++++++++++ modules/xalloc | 2 +- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index aff0e87fc..857b7048f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2021-04-18 Paul Eggert =20 + xalloc: new function xreallocarray + This effectively replaces xnmalloc, which perhaps should be deprecated. + The name xreallocarray should be easier to remember now that + reallocarray is a standard GNU function. + * lib/xalloc.h [GNULIB_XALLOC]: Do not include xalloc-oversized.h. + (xnmalloc, xnrealloc, x2nrealloc): Simplify by using xreallocarray. + * lib/xmalloc.c (xreallocarray): New function. + * modules/xalloc (Depends-on): Add reallocarray; + remove xalloc-oversized. + group-member: simplify via realloc-gnu * lib/group-member.c, modules/group-member: Simplify similarly to backupfile. diff --git a/lib/xalloc.h b/lib/xalloc.h index b08ab4e07..6cd7a680c 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -24,7 +24,6 @@ #if GNULIB_XALLOC # include "idx.h" # include "intprops.h" -# include "xalloc-oversized.h" #endif =20 #ifndef _GL_INLINE_HEADER_BEGIN @@ -62,6 +61,8 @@ void *xcalloc (size_t n, size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); void *xrealloc (void *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); +void *xreallocarray (void *p, size_t n, size_t s) + _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); void *x2realloc (void *p, size_t *pn); void *xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min, ptrdiff_t nitems_max, idx_t item_size); @@ -104,11 +105,10 @@ XALLOC_INLINE void *xnmalloc (size_t n, size_t s) XALLOC_INLINE void * xnmalloc (size_t n, size_t s) { - if (xalloc_oversized (n, s)) - xalloc_die (); - return xmalloc (n * s); + return xreallocarray (NULL, n, s); } =20 +/* FIXME: Deprecate this in favor of xreallocarray? */ /* Change the size of an allocated block of memory P to an array of N objects each of S bytes, with error checking. S must be nonzero. */ =20 @@ -117,9 +117,7 @@ XALLOC_INLINE void *xnrealloc (void *p, size_t n, siz= e_t s) XALLOC_INLINE void * xnrealloc (void *p, size_t n, size_t s) { - if (xalloc_oversized (n, s)) - xalloc_die (); - return xrealloc (p, n * s); + return xreallocarray (p, n, s); } =20 /* If P is null, allocate a block of at least *PN such objects; @@ -202,10 +200,7 @@ x2nrealloc (void *p, size_t *pn, size_t s) xalloc_die (); } =20 - xalloc_count_t nbytes; - if (INT_MULTIPLY_WRAPV (n, s, &nbytes)) - xalloc_die (); - p =3D xrealloc (p, nbytes); + p =3D xreallocarray (p, n, s); *pn =3D n; return p; } @@ -241,10 +236,17 @@ xrealloc (T *p, size_t s) return (T *) xrealloc ((void *) p, s); } =20 +template inline T * +xreallocarray (T *p, size_t n, size_t s) +{ + return (T *) xreallocarray ((void *) p, n, s); +} + +/* FIXME: Deprecate this in favor of xreallocarray? */ template inline T * xnrealloc (T *p, size_t n, size_t s) { - return (T *) xnrealloc ((void *) p, n, s); + return xreallocarray (p, n, s); } =20 template inline T * diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 39ce893ad..88698fade 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -50,6 +50,18 @@ xrealloc (void *p, size_t n) return r; } =20 +/* Change the size of an allocated block of memory P to an array of N + objects each of S bytes, with error checking. */ + +void * +xreallocarray (void *p, size_t n, size_t s) +{ + void *r =3D reallocarray (p, n, s); + if (!r && (!p || (n && s))) + xalloc_die (); + return r; +} + /* If P is null, allocate a block of at least *PN bytes; otherwise, reallocate P so that it contains more than *PN bytes. *PN must be nonzero unless P is null. Set *PN to the new block's size, and diff --git a/modules/xalloc b/modules/xalloc index 000933e94..0dbae1c86 100644 --- a/modules/xalloc +++ b/modules/xalloc @@ -15,9 +15,9 @@ intprops malloc-gnu minmax realloc-gnu +reallocarray stdint xalloc-die -xalloc-oversized =20 configure.ac: gl_XALLOC --=20 2.27.0