From 30fd71f5acbb8979b980e8df15cabd2ba5b70cca Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Tue, 12 Mar 2024 23:21:16 -0400 Subject: [PATCH] fix(lib): avoid possible UB of calling memset on a null ptr when 0 is passed into `array_grow_by` --- lib/src/array.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/src/array.h b/lib/src/array.h index 186ba673..15a3b233 100644 --- a/lib/src/array.h +++ b/lib/src/array.h @@ -66,9 +66,12 @@ extern "C" { /// Increase the array's size by `count` elements. /// New elements are zero-initialized. #define array_grow_by(self, count) \ - (_array__grow((Array *)(self), count, array_elem_size(self)), \ - memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)), \ - (self)->size += (count)) + do { \ + if ((count) == 0) break; \ + _array__grow((Array *)(self), count, array_elem_size(self)); \ + memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ + (self)->size += (count); \ + } while (0) /// Append all elements from one array to the end of another. #define array_push_all(self, other) \