mem: do not permit zero-sized allocations (FLU-01-002)
note: security audit report by Cure53/CNCF
@@ -55,6 +55,10 @@ static inline ALLOCSZ_ATTR(1)
void *flb_malloc(const size_t size) {
void *aux;
+ if (size == 0) {
+ return NULL;
+ }
+
aux = malloc(size);
if (flb_unlikely(!aux && size)) {
return NULL;
@@ -67,6 +71,10 @@ static inline ALLOCSZ_ATTR(1)
void *flb_calloc(size_t n, const size_t size) {
void *buf;
+ if (size == 0) {
+ return NULL;
+ }
+
buf = calloc(n, size);
if (flb_unlikely(!buf)) {
return NULL;
@@ -80,6 +88,13 @@ void *flb_realloc(void *ptr, const size_t size)
{
void *aux;
+ if (size == 0) {
+ if (ptr) {
+ free(ptr);
+ }
+ return NULL;
+ }
+
aux = realloc(ptr, size);
if (flb_unlikely(!aux && size)) {
return NULL;
