From c30e6bb022faf6923f93e6414920bf6e182a25ff Mon Sep 17 00:00:00 2001 From: 94xhn <87560781+94xhn@users.noreply.github.com> Date: Wed, 15 Jul 2026 05:17:12 +0800 Subject: [PATCH] Fix CMock_Guts_MemBytesCapacity() integer underflow CMock_Guts_MemBytesCapacity() returned sizeof(CMock_Guts_Buffer), which is the size of the pointer variable itself (not the buffer it points to), minus CMOCK_MEM_ALIGN_SIZE. On a typical 64-bit build this is 8 - CMOCK_MEM_ALIGN_SIZE, which underflows to a huge garbage value whenever CMOCK_MEM_ALIGN_SIZE > sizeof(void*) (e.g. when it defaults to sizeof(max_align_t) on C11+ toolchains). Use CMock_Guts_BufferSize instead, which already tracks the real buffer size (including growth via realloc under CMOCK_MEM_DYNAMIC). This makes MemBytesCapacity() == MemBytesUsed() + MemBytesFree() by construction, matching the two sibling accessors. --- src/cmock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmock.c b/src/cmock.c index 190284a2..4aeeba9d 100644 --- a/src/cmock.c +++ b/src/cmock.c @@ -197,7 +197,7 @@ void* CMock_Guts_GetAddressFor(CMOCK_MEM_INDEX_TYPE index) *-------------------------------------------------------*/ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesCapacity(void) { - return (sizeof(CMock_Guts_Buffer) - CMOCK_MEM_ALIGN_SIZE); + return (CMock_Guts_BufferSize - CMOCK_MEM_ALIGN_SIZE); } /*-------------------------------------------------------