From 7bceefbbd85a5b873e46299a4a55f50e2b56294a Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Mon, 17 Oct 2016 08:08:18 +0300 Subject: [PATCH] Workaround 'overflow in pointer subtraction' cppcheck warning * src/atomic_ops_malloc.c (AO_free): Do not perform subtraction from "p" argument if its value is null; change type of "base" local variable from char* to AO_t*. --- src/atomic_ops_malloc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/atomic_ops_malloc.c b/src/atomic_ops_malloc.c index 6d4d81a..3f1881b 100644 --- a/src/atomic_ops_malloc.c +++ b/src/atomic_ops_malloc.c @@ -304,11 +304,13 @@ AO_malloc(size_t sz) void AO_free(void *p) { - char *base = (char *)p - sizeof(AO_t); + AO_t *base; int log_sz; if (0 == p) return; - log_sz = (int)(*(AO_t *)base); + + base = (AO_t *)p - 1; + log_sz = (int)(*base); # ifdef AO_TRACE_MALLOC fprintf(stderr, "%x: AO_free(%p sz:%lu)\n", (int)pthread_self(), p, (unsigned long)(log_sz > LOG_MAX_SIZE? log_sz : (1 << log_sz))); @@ -316,5 +318,5 @@ AO_free(void *p) if (log_sz > LOG_MAX_SIZE) AO_free_large(p); else - AO_stack_push(AO_free_list+log_sz, (AO_t *)base); + AO_stack_push(AO_free_list + log_sz, base); } -- 2.40.0