Skip to content

Commit 24ced30

Browse files
jimmunnssdrive
authored andcommitted
util/mem: fix NULL dereference in nvme_realloc() on allocation failure
If nvme_alloc() fails and returns NULL, the subsequent memcpy() would dereference a NULL pointer, resulting in undefined behavior. Add a NULL check on the result of nvme_alloc() and return NULL early, consistent with standard realloc() semantics where the original pointer remains valid on failure. Signed-off-by: Jim Munn <[email protected]>
1 parent e89daf6 commit 24ced30

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

util/mem.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void *nvme_realloc(void *p, size_t len)
2929
size_t old_len = malloc_usable_size(p);
3030

3131
void *result = nvme_alloc(len);
32+
if (!result)
33+
return NULL;
3234

3335
if (p) {
3436
memcpy(result, p, min(old_len, len));

0 commit comments

Comments
 (0)