From: Paul Eggert <eggert@cs.ucla.edu>
To: bug-gnulib@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH] xmalloca, etc.: avoid unlikely trap
Date: Sat, 24 Apr 2021 18:14:19 -0700 [thread overview]
Message-ID: <20210425011419.338896-1-eggert@cs.ucla.edu> (raw)
* 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 <eggert@cs.ucla.edu>
- 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 @@
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)).
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)
--
2.27.0
reply other threads:[~2021-04-25 1:14 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210425011419.338896-1-eggert@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=bug-gnulib@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).