From bc75c4db7bfa9ef095ec49d87b2f98f9f6d16198 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 17 Jun 2019 00:23:03 -0400 Subject: [PATCH] realloc: use copy_size to check for canaries This avoids unnecessarily copying the canary when doing a realloc from a small size to a large size. It also avoids trying to copy a non-existent canary out of a zero-size allocation, which are memory protected. --- h_malloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/h_malloc.c b/h_malloc.c index 62522af..29187bf 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1474,7 +1474,7 @@ EXPORT void *h_realloc(void *old, size_t size) { return NULL; } size_t copy_size = min(size, old_size); - if (size > 0 && size <= max_slab_size_class) { + if (copy_size > 0 && copy_size <= max_slab_size_class) { copy_size -= canary_size; } memcpy(new, old, copy_size);