alloc-util: add extra overflow checks to GREEDY_REALLOC()

This commit is contained in:
Lennart Poettering
2019-03-20 10:48:33 +01:00
parent e5e21a0507
commit 23964f7faf
+9 -5
View File
@@ -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;
/* check for overflows */
if (a < size * need)
if (_unlikely_(need > SIZE_MAX/2)) /* Overflow check */
return NULL;
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;