Skip to content

Commit 2757641

Browse files
committed
tests: only copy when ptr is non-NULL
Avoid passing a `NULL` ptr to `memcpy` -- that's UB (even if size is 0)
1 parent 8e5281c commit 2757641

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

tests/clar/clar_libgit2_alloc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ static void *cl__realloc(void *ptr, size_t size, const char *file, int line)
7373

7474
if (p)
7575
memcpy(&copybytes, p - sizeof(size_t), sizeof(size_t));
76+
7677
if (copybytes > size)
7778
copybytes = size;
7879

7980
if ((new = cl__malloc(size, file, line)) == NULL)
8081
goto out;
81-
memcpy(new, p, copybytes);
82-
cl__free(p);
82+
83+
if (p) {
84+
memcpy(new, p, copybytes);
85+
cl__free(p);
86+
}
8387

8488
out:
8589
return new;

0 commit comments

Comments
 (0)