From 9489490a693ec5d1e3b49eecedb0ca5511568665 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Fri, 13 Jun 2014 18:43:22 +0200 Subject: util: add realloc_multiply() helper This is similar to malloc_multiply() and friends. It is realloc() with a multiplication-overflow check. --- src/shared/util.h | 7 +++++++ 1 file changed, 7 insertions(+) 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; -- cgit v1.2.3-54-g00ecf