From: Paul Eggert Date: Thu, 29 Dec 2016 09:16:51 +0000 (-0800) Subject: xalloc: x2nrealloc check for ptrdiff_t overflow X-Git-Tag: v1.0~6445 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=8e63aebd5d3e398a986dcc5cfaaa5409bca30d12;p=gnulib.git xalloc: x2nrealloc check for ptrdiff_t overflow * lib/xalloc.h (x2nrealloc): Check for ptrdiff_t overflow, too. * modules/xalloc, modules/xvasprintf (Depends-on): Add stdint. --- diff --git a/ChangeLog b/ChangeLog index b1f41bb0ff..92dda3acce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2016-12-29 Paul Eggert + + xalloc: x2nrealloc check for ptrdiff_t overflow + * lib/xalloc.h (x2nrealloc): Check for ptrdiff_t overflow, too. + * modules/xalloc, modules/xvasprintf (Depends-on): Add stdint. + 2016-12-24 Bruno Haible lock test: Fix performance problem on multi-core machines. diff --git a/lib/xalloc.h b/lib/xalloc.h index a4fddbfb5b..3aad4e3110 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -19,6 +19,7 @@ #define XALLOC_H_ #include +#include #include "xalloc-oversized.h" @@ -197,10 +198,11 @@ x2nrealloc (void *p, size_t *pn, size_t s) else { /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0. - Check for overflow, so that N * S stays in size_t range. - The check may be slightly conservative, but an exact check isn't - worth the trouble. */ - if ((size_t) -1 / 3 * 2 / s <= n) + Check for overflow, so that N * S stays in both ptrdiff_t and + size_t range. The check may be slightly conservative, but an + exact check isn't worth the trouble. */ + if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s + <= n) xalloc_die (); n += n / 2 + 1; } diff --git a/modules/xalloc b/modules/xalloc index c6c20836b5..b107da93bb 100644 --- a/modules/xalloc +++ b/modules/xalloc @@ -8,6 +8,7 @@ m4/xalloc.m4 Depends-on: extern-inline +stdint xalloc-die xalloc-oversized diff --git a/modules/xvasprintf b/modules/xvasprintf index a931d7c919..410b45f0a7 100644 --- a/modules/xvasprintf +++ b/modules/xvasprintf @@ -17,6 +17,7 @@ vasprintf xalloc-die xsize stdarg +stdint errno configure.ac: