]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
alloca: add an overflow check too
authorLennart Poettering <lennart@poettering.net>
Fri, 27 Apr 2018 12:27:14 +0000 (14:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 27 Apr 2018 12:29:06 +0000 (14:29 +0200)
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.

src/basic/alloc-util.h

index 88cd6b0bc25d682c0a45493040444c0bc8c8f89a..bae6a284513d98ac806b38cf7f3170d1e354e447 100644 (file)
 
 #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)))