chiark / gitweb /
util: fix multiply-alloc helpers with size==0
authorDavid Herrmann <dh.herrmann@gmail.com>
Fri, 13 Jun 2014 16:38:15 +0000 (18:38 +0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Mon, 16 Jun 2014 13:22:57 +0000 (15:22 +0200)
Passing 0 to malloc() is not required to return NULL. Therefore, don't
bail out if "b" is 0. This is not of importance to the existing helpers,
but the upcoming realloc_multiply() requires this. To keep consistence, we
keep the same behavior for the other helpers.

src/shared/util.h

index e8552410c06114377232191b1155090083be117d..5a2f10387e2a063b8217b185b5d3b0b4c3857df5 100644 (file)
@@ -666,14 +666,14 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
 #define _cleanup_close_pair_ _cleanup_(close_pairp)
 
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
 #define _cleanup_close_pair_ _cleanup_(close_pairp)
 
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
-        if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
+        if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
                 return NULL;
 
         return malloc(a * b);
 }
 
 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) {
                 return NULL;
 
         return malloc(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))
+        if (_unlikely_(b != 0 && a > ((size_t) -1) / b))
                 return NULL;
 
         return memdup(p, a * b);
                 return NULL;
 
         return memdup(p, a * b);