From 4095c3afa8c0c3fe8b8c9b8e7187d3adf62b8bc8 Mon Sep 17 00:00:00 2001 From: Matt Hartley Date: Mon, 26 Aug 2024 19:38:03 -0500 Subject: [PATCH] initialized all allocated bytes in check_error to 0xAA to ensure that clang's memory sanitizer doesn't exit due to these allocations. changed function signature of erealloc --- src/check_error.c | 5 ++++- src/check_error.h | 2 +- src/check_list.c | 2 +- src/check_str.c | 8 +++++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/check_error.c b/src/check_error.c index 56a7537d..92160707 100644 --- a/src/check_error.c +++ b/src/check_error.c @@ -62,15 +62,18 @@ void *emalloc(size_t n) p = malloc(n); if(p == NULL) eprintf("malloc of " CK_FMT_ZU " bytes failed:", __FILE__, __LINE__ - 2, n); + memset(p, 0xAA, n); return p; } -void *erealloc(void *ptr, size_t n) +void *erealloc(void *ptr, size_t old_n, size_t n) { void *p; p = realloc(ptr, n); if(p == NULL) eprintf("realloc of " CK_FMT_ZU " bytes failed:", __FILE__, __LINE__ - 2, n); + if(n > old_n) + memset(p+old_n, 0xAA, n-old_n); return p; } diff --git a/src/check_error.h b/src/check_error.h index 0dc5bb9f..d3a5230f 100644 --- a/src/check_error.h +++ b/src/check_error.h @@ -34,6 +34,6 @@ void eprintf(const char *fmt, const char *file, int line, ...) CK_ATTRIBUTE_NORETURN CK_ATTRIBUTE_FORMAT(printf, 1, 4); /* malloc or die */ void *emalloc(size_t n); -void *erealloc(void *, size_t n); +void *erealloc(void *, size_t old_n, size_t n); #endif /*ERROR_H */ diff --git a/src/check_list.c b/src/check_list.c index d93f51c8..d2fe2bbb 100644 --- a/src/check_list.c +++ b/src/check_list.c @@ -46,8 +46,8 @@ static void maybe_grow(List * lp) { if(lp->n_elts >= lp->max_elts) { + lp->data = (void **)erealloc(lp->data, lp->max_elts * sizeof(lp->data[0]), lp->max_elts * LGROW * sizeof(lp->data[0])); lp->max_elts *= LGROW; - lp->data = (void **)erealloc(lp->data, lp->max_elts * sizeof(lp->data[0])); } } diff --git a/src/check_str.c b/src/check_str.c index 8dabdcc6..94ab29c9 100644 --- a/src/check_str.c +++ b/src/check_str.c @@ -78,6 +78,7 @@ char *ck_strdup_printf(const char *fmt, ...) { /* Guess we need no more than 100 bytes. */ size_t size = 100; + size_t new_size; char *p; va_list ap; @@ -96,11 +97,12 @@ char *ck_strdup_printf(const char *fmt, ...) /* Else try again with more space. */ if(n > -1) /* C99 conform vsnprintf() */ - size = (size_t) n + 1; /* precisely what is needed */ + new_size = (size_t) n + 1; /* precisely what is needed */ else /* glibc 2.0 */ - size *= 2; /* twice the old size */ + new_size *= 2; /* twice the old size */ - p = (char *)erealloc(p, size); + p = (char *)erealloc(p, size, new_size); + size = new_size; } }