]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
alloc-util: return NULL if 0-sized allocation is requested
authorDavid Tardon <dtardon@redhat.com>
Tue, 9 Oct 2018 14:53:43 +0000 (16:53 +0200)
committerDavid Tardon <dtardon@redhat.com>
Fri, 12 Oct 2018 12:51:35 +0000 (14:51 +0200)
That would almost certainly be an error (e.g., an overflow in computing
_need_), so it's better to fail.

src/basic/alloc-util.h

index ebe42889ea9f6d1918ed12fc886d0433dfab6326..62b591117b9d92fd30833420e79c75633028cac2 100644 (file)
@@ -52,8 +52,11 @@ static inline void freep(void *p) {
 
 #define _cleanup_free_ _cleanup_(freep)
 
+/* Checks the size arguments of allocation functions for overflow in multiplication. In addition, checks if either of
+ * them is 0; that is almost certainly an error (e.g., an overflow in computing _need_), so it's better to fail (and
+ * we cannot leave this check to malloc, because the behavior of malloc(0) is impl. specific). */
 static inline bool size_multiply_overflow(size_t size, size_t need) {
-        return _unlikely_(need != 0 && size > (SIZE_MAX / need));
+        return _unlikely_(need == 0 || size == 0 || size > (SIZE_MAX / need));
 }
 
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {