From f8c6b7a2d1a28eca869008e56521ed5834d3bec1 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 15 Dec 2016 09:59:21 -0800 Subject: [PATCH] xalloc: do not exceed PTRDIFF_MAX * lib/xmalloc.c (xcalloc) [HAVE_GNU_CALLOC]: Do not omit xalloc_oversized check, since objects larger than PTRDIFF_MAX bytes have pointer-subtraction problems. --- ChangeLog | 5 +++++ lib/xmalloc.c | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 07c4d65396..bee9a1728e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2016-12-15 Paul Eggert + xalloc: do not exceed PTRDIFF_MAX + * lib/xmalloc.c (xcalloc) [HAVE_GNU_CALLOC]: Do not omit + xalloc_oversized check, since objects larger than PTRDIFF_MAX + bytes have pointer-subtraction problems. + malloca: do not exceed PTRDIFF_MAX * lib/malloca.h: Include xalloc-oversized. (nmalloca): Use xalloc_oversized instead of rolling our own. diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 429b50d994..7d9c077729 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -93,11 +93,11 @@ void * xcalloc (size_t n, size_t s) { void *p; - /* Test for overflow, since some calloc implementations don't have - proper overflow checks. But omit overflow and size-zero tests if - HAVE_GNU_CALLOC, since GNU calloc catches overflow and never - returns NULL if successful. */ - if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s)) + /* Test for overflow, since objects with size greater than + PTRDIFF_MAX cause pointer subtraction to go awry. Omit size-zero + tests if HAVE_GNU_CALLOC, since GNU calloc never returns NULL if + successful. */ + if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0))) xalloc_die (); return p; -- 2.39.5