From: Lennart Poettering Date: Fri, 27 Apr 2018 12:27:14 +0000 (+0200) Subject: alloca: add an overflow check too X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=7ebe63f00f55a754c07c020c80fbbba799c30fda;p=elogind.git alloca: add an overflow check too Of course, alloca() shouldn't be used with anything that can grow without bounds anyway, but let's better safe than sorry, and catch this early. Since alloca() is not supposed to return an error we trigger an assert() instead, which is still better than heap trickery. --- diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 88cd6b0bc..bae6a2845 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -18,9 +18,17 @@ #define new0(t, n) ((t*) calloc((n), sizeof(t))) -#define newa(t, n) ((t*) alloca(sizeof(t)*(n))) +#define newa(t, n) \ + ({ \ + assert(!size_multiply_overflow(sizeof(t), n)); \ + (t*) alloca(sizeof(t)*(n)); \ + }) -#define newa0(t, n) ((t*) alloca0(sizeof(t)*(n))) +#define newa0(t, n) \ + ({ \ + assert(!size_multiply_overflow(sizeof(t), n)); \ + (t*) alloca0(sizeof(t)*(n)); \ + }) #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))