Fix CMock_Guts_MemBytesCapacity() integer underflow#536
Merged
mvandervoord merged 1 commit intoJul 16, 2026
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🍍
Bug
CMock_Guts_MemBytesCapacity()(src/cmock.c) returns:CMock_Guts_Bufferis declared asstatic unsigned char* CMock_Guts_Buffer, sosizeof(CMock_Guts_Buffer)is the size of the pointer (8 bytes on a typical 64-bit build), not the size of the buffer it points to. WheneverCMOCK_MEM_ALIGN_SIZE > sizeof(void*)-- which happens by default on C11+ toolchains, sinceCMOCK_MEM_ALIGN_SIZEauto-upgrades tosizeof(max_align_t)unless the user overridesCMOCK_MEM_ALIGN(see src/cmock_internals.h) -- the subtraction underflows andCMock_Guts_MemBytesCapacity()returns a huge garbage value instead of the real capacity.Confirmed by compiling the unmodified
src/cmock.cwithvendor/unity/src/unity.c(GCC,-std=c11, noCMOCK_MEM_ALIGN/CMOCK_MEM_SIZEoverrides):sizeof(void*)=8,sizeof(max_align_t)=32, soCMOCK_MEM_ALIGN_SIZE=32, andCMock_Guts_MemBytesCapacity()returns18446744073709551592(0xffffffffffffffe8) instead of the true capacity of32768.This is why CI doesn't catch it:
test/c/TestCMockC.ymlpinsCMOCK_MEM_ALIGN=2, which keepsCMOCK_MEM_ALIGN_SIZEbelowsizeof(void*)so no underflow occurs there, and the self-test only assertsCapacity() <= CMOCK_MEM_SIZE(an upper bound), so a wrong-but-small value still passes silently.Fix
Use
CMock_Guts_BufferSizeinstead, which already tracks the real buffer size in both the default/static branch and theCMOCK_MEM_DYNAMICbranch (including growth viarealloc). This makesMemBytesCapacity() == MemBytesUsed() + MemBytesFree()by construction, matching the two sibling accessors that already useCMock_Guts_BufferSize/CMock_Guts_FreePtrcorrectly.Testing
MemBytesCapacity() == MemBytesUsed() + MemBytesFree()in the default/static config, and inCMOCK_MEM_DYNAMICmode both before and after arealloc-driven buffer growth.test/c/TestCMockC.csuite with the exactCMOCK_MEM_STATIC/CMOCK_MEM_ALIGN=2/CMOCK_MEM_INDEX_TYPE=intconfiguration fromtest/c/TestCMockC.yml: all 9 tests still pass with this fix (no regression).