diff options
author | David Herrmann <dh.herrmann@gmail.com> | 2014-06-13 18:43:22 +0200 |
---|---|---|
committer | David Herrmann <dh.herrmann@gmail.com> | 2014-06-16 15:23:06 +0200 |
commit | 9489490a693ec5d1e3b49eecedb0ca5511568665 (patch) | |
tree | 9985e44026281d04a90a2c865947844119ca68d9 /src/shared | |
parent | 368504f485d09b9fd48b7538e71981f648eb32bb (diff) |
util: add realloc_multiply() helper
This is similar to malloc_multiply() and friends. It is realloc() with a
multiplication-overflow check.
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/shared/util.h b/src/shared/util.h index 5a2f10387e..8ddd1a5daf 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -672,6 +672,13 @@ _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) return malloc(a * b); } +_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) { + if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) + return NULL; + + return realloc(p, a * b); +} + _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) { if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) return NULL; |