summaryrefslogtreecommitdiff
path: root/src/basic/alloc-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-03-20 10:31:13 +0100
committerLennart Poettering <lennart@poettering.net>2019-03-20 10:48:33 +0100
commit23964f7faf86df7fd297dc5b0a9f1dfb03a0eb56 (patch)
tree718e6c40ec7f7a2bdf2223905188c1818f7654e6 /src/basic/alloc-util.c
parente5e21a05076e60fe46eab4eb040735250ae3b814 (diff)
downloadsystemd-23964f7faf86df7fd297dc5b0a9f1dfb03a0eb56.tar.gz
alloc-util: add extra overflow checks to GREEDY_REALLOC()
Diffstat (limited to 'src/basic/alloc-util.c')
-rw-r--r--src/basic/alloc-util.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c
index 15b67665d2..f4bd33f4e0 100644
--- a/src/basic/alloc-util.c
+++ b/src/basic/alloc-util.c
@@ -48,13 +48,17 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
if (*allocated >= need)
return *p;
- newalloc = MAX(need * 2, 64u / size);
- a = newalloc * size;
+ if (_unlikely_(need > SIZE_MAX/2)) /* Overflow check */
+ return NULL;
- /* check for overflows */
- if (a < size * need)
+ newalloc = need * 2;
+ if (size_multiply_overflow(newalloc, size))
return NULL;
+ a = newalloc * size;
+ if (a < 64) /* Allocate at least 64 bytes */
+ a = 64;
+
q = realloc(*p, a);
if (!q)
return NULL;