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.9 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, 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 3DEBA1F5AE for ; Sun, 25 Apr 2021 01:14:35 +0000 (UTC) Received: from localhost ([::1]:40330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1laTM2-0007aD-5w for normalperson@yhbt.net; Sat, 24 Apr 2021 21:14:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49836) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1laTLy-0007Zr-Gr for bug-gnulib@gnu.org; Sat, 24 Apr 2021 21:14:30 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:46924) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1laTLw-00042z-74 for bug-gnulib@gnu.org; Sat, 24 Apr 2021 21:14:29 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 3B1451600B2 for ; Sat, 24 Apr 2021 18:14:25 -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 ysppOK1glJUK; Sat, 24 Apr 2021 18:14:24 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 4EADE1600A7; Sat, 24 Apr 2021 18:14:24 -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 MZLPStMZ9GNp; Sat, 24 Apr 2021 18:14:24 -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 F3E94160099; Sat, 24 Apr 2021 18:14:23 -0700 (PDT) From: Paul Eggert To: bug-gnulib@gnu.org Subject: [PATCH] xmalloca, etc.: avoid unlikely trap Date: Sat, 24 Apr 2021 18:14:19 -0700 Message-Id: <20210425011419.338896-1-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.27.0 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" * lib/xmalloca.h (xnmalloca): Use size_t, not ptrdiff_t, to avoid implementation-defined behavior (which could include a trap) if N is 0 and S exceeds PTRDIFF_MAX. * lib/xalloc-oversized.h: Adjust comment to match. --- ChangeLog | 8 +++++++- lib/xalloc-oversized.h | 4 ++-- lib/xmalloca.h | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 63471166c..f91f2e3df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,13 @@ 2021-04-24 Paul Eggert =20 - xmalloca, etc.: fix some xalloc-oversized issues + xmalloca, etc.: avoid unlikely trap * lib/malloca.h (nmalloca): + * lib/xmalloca.h (xnmalloca): + Use size_t, not ptrdiff_t, to avoid implementation-defined + behavior (which could include a trap) if N is 0 and S exceeds + PTRDIFF_MAX. + * lib/xalloc-oversized.h: Adjust comment to match. + * lib/xmalloca.h (xnmalloca): Convert S to ptrdiff_t to avoid arithmetic overflow if N and S are both narrower than ptrdiff_t. * lib/xalloc-oversized.h (xalloc_oversized): diff --git a/lib/xalloc-oversized.h b/lib/xalloc-oversized.h index 62f1ae94b..2619a2aab 100644 --- a/lib/xalloc-oversized.h +++ b/lib/xalloc-oversized.h @@ -38,8 +38,8 @@ =20 Warning: (xalloc_oversized (N, S) ? NULL : malloc (N * S)) can misbehave if N and S are both narrower than ptrdiff_t and size_t, - and can be rewritten as (xalloc_oversized (N, S) ? NULL : malloc - (N * (ptrdiff_t) S)) or similarly with size_t. + and can be rewritten as (xalloc_oversized (N, S) ? NULL + : malloc (N * (size_t) S)). =20 This is a macro, not a function, so that it works even if an argument exceeds MAX (PTRDIFF_MAX, SIZE_MAX). */ diff --git a/lib/xmalloca.h b/lib/xmalloca.h index c7815f617..dbc648697 100644 --- a/lib/xmalloca.h +++ b/lib/xmalloca.h @@ -54,7 +54,7 @@ extern void * xmmalloca (size_t n); #if HAVE_ALLOCA /* Rely on xmalloca (SIZE_MAX) calling xalloc_die (). */ # define xnmalloca(n, s) \ - xmalloca (xalloc_oversized (n, s) ? (size_t) (-1) : (n) * (ptrdiff_t= ) (s)) + xmalloca (xalloc_oversized (n, s) ? (size_t) (-1) : (n) * (size_t) (= s)) #else # define xnmalloca(n, s) \ xnmalloc (n, s) --=20 2.27.0