Discussion:
[PATCH] xalloc: x2nrealloc check for ptrdiff_t overflow
(too old to reply)
Paul Eggert
2016-12-29 09:17:39 UTC
Permalink
Raw Message
* lib/xalloc.h (x2nrealloc): Check for ptrdiff_t overflow, too.
* modules/xalloc, modules/xvasprintf (Depends-on): Add stdint.
---
ChangeLog | 6 ++++++
lib/xalloc.h | 10 ++++++----
modules/xalloc | 1 +
modules/xvasprintf | 1 +
4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b1f41bb..92dda3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2016-12-29 Paul Eggert <***@cs.ucla.edu>
+
+ 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 <***@clisp.org>

lock test: Fix performance problem on multi-core machines.
diff --git a/lib/xalloc.h b/lib/xalloc.h
index a4fddbf..3aad4e3 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -19,6 +19,7 @@
#define XALLOC_H_

#include <stddef.h>
+#include <stdint.h>

#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 c6c2083..b107da9 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 a931d7c..410b45f 100644
--- a/modules/xvasprintf
+++ b/modules/xvasprintf
@@ -17,6 +17,7 @@ vasprintf
xalloc-die
xsize
stdarg
+stdint
errno

configure.ac:
--
2.7.4
Loading...