Details
Description
bson_malloc() aborts on a zero length malloc.
Compilers aren't required to return a ptr for 0 length, they may return NULL instead. Solution is to use same test as used in bson_malloc0 (somebody appears to have looked into this previously but it didn't get applied to bson_malloc).
This is relevant because there is code in the mongodb c driver that expects to get a valid pointer on a 0 length call to malloc (mongoc_set_for_each() is one example). On a susceptible compiler you can't even get past mongoc_client_new() without the patch applied.
Corrected version:
void * |
bson_malloc (size_t num_bytes) /* IN */ |
{
|
void *mem = NULL; |
|
|
if (BSON_LIKELY (num_bytes)) { |
if (BSON_UNLIKELY (!(mem = gMemVtable.malloc (num_bytes)))) { |
abort (); |
}
|
}
|
|
|
return mem; |
}
|