diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-05-15 11:04:37 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-05-15 11:04:37 +0100 |
commit | 27576416206f696edb16cf2e22271953903e731f (patch) | |
tree | 782803d331a3152c53abf2014d7f9cf25136ded5 /tests/clar/clar_libgit2_alloc.c | |
parent | 8e5281c88b860cce7a6a2f4a00c28b6fb00720b0 (diff) | |
download | libgit2-pks/test-allocator.tar.gz |
tests: only copy when `ptr` is non-NULLpks/test-allocator
Avoid passing a `NULL` ptr to `memcpy` -- that's UB (even if size is 0)
Diffstat (limited to 'tests/clar/clar_libgit2_alloc.c')
-rw-r--r-- | tests/clar/clar_libgit2_alloc.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/tests/clar/clar_libgit2_alloc.c b/tests/clar/clar_libgit2_alloc.c index 7abc998ce..e93037923 100644 --- a/tests/clar/clar_libgit2_alloc.c +++ b/tests/clar/clar_libgit2_alloc.c @@ -73,13 +73,17 @@ static void *cl__realloc(void *ptr, size_t size, const char *file, int line) if (p) memcpy(©bytes, p - sizeof(size_t), sizeof(size_t)); + if (copybytes > size) copybytes = size; if ((new = cl__malloc(size, file, line)) == NULL) goto out; - memcpy(new, p, copybytes); - cl__free(p); + + if (p) { + memcpy(new, p, copybytes); + cl__free(p); + } out: return new; |