summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2021-04-07 10:28:20 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2021-04-07 10:29:19 -0700
commita4f1d2cae56a495799f4da7e414105ad536b3d01 (patch)
treeb87a989f124bbad559bf84ede0951413bffe46ec /lib
parent7e90795b4061e4d023c8ab4937d988dbbdbf9d15 (diff)
downloadgnulib-a4f1d2cae56a495799f4da7e414105ad536b3d01.tar.gz
xalloc: simplify integer overflow test
* lib/xalloc.h (x2nrealloc): Simplify integer overflow detection. This is easier to maintain, and (unlike the old code) exact.
Diffstat (limited to 'lib')
-rw-r--r--lib/xalloc.h16
1 files changed, 6 insertions, 10 deletions
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 6e7de60da9..230ea9d203 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -193,22 +193,18 @@ x2nrealloc (void *p, size_t *pn, size_t s)
n = DEFAULT_MXFAST / s;
n += !n;
}
- if (xalloc_oversized (n, s))
- xalloc_die ();
}
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 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)
+ /* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */
+ if (INT_ADD_WRAPV (n, (n >> 1) + 1, &n))
xalloc_die ();
- n += n / 2 + 1;
}
- p = xrealloc (p, n * s);
+ xalloc_count_t nbytes;
+ if (INT_MULTIPLY_WRAPV (n, s, &nbytes))
+ xalloc_die ();
+ p = xrealloc (p, nbytes);
*pn = n;
return p;
}