Fix debug block size overflow checks#68
Closed
janrysavy wants to merge 1 commit intopleriche:masterfrom
Closed
Conversation
Debug-mode allocations add header and footer overhead before passing the request to the normal allocator. If that actual debug block size cannot be represented in NativeInt, the debug wrapper must fail before populating or moving debug metadata. This matters most for ReallocMem, where a failed resize must leave the original block valid. Add a checked debug block size helper and use it before debug GetMem allocates a block and before debug ReallocMem decides whether a block can be resized in place or must be replaced. A standalone reproducer covering unrepresentable debug block sizes was validated locally with RAD Studio 37.0 for Win32 and Win64 and is kept outside the upstream PR tree.
Owner
|
Thank you very much for the report. I have pushed a fix for the overflow when requesting a new size close to Max(NativeInt). I am on the fence whether it is worth adding checks for the negative size case since handling negative sizes gracefully is not in the implicit contract: These functions should all be called through the standard memory manager interface, which already checks for sizes <= 0. If I add <= 0 checks in the debug calls then I would also need to add it to the non-debug calls (which are also affected). In the latter case performance matters, so I would rather let the memory manager interface deal with the negative values instead and not perform duplicate checks. |
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.
Summary
This is a second small follow-up PR from the same review pass as #67.
The first PR fixed a debug-mode
AllocMemheader lookup issue. This PR addresses another debug-mode edge case: calculating the actual debug block size after adding header/footer overhead without first checking whether that size is representable inNativeInt.The fix is intentionally limited to debug-mode allocation/reallocation paths.
Problem
Debug-mode allocations add metadata around the user block:
CDebugBlockHeaderSizeThe previous code added that overhead directly to the requested user size:
and debug
ReallocMemused the same unchecked arithmetic when deciding whether the existing block could be resized in place:If the actual debug block size cannot be represented in
NativeInt, the calculation can wrap before the normal allocator or realloc path sees the request.The most important case is debug
ReallocMem: a failed resize must leave the original block valid. With the unchecked in-place size test, an overflowed calculated size can make the code treat the existing block as large enough and mutate debug metadata, includingUserSizeand the debug footer location, before the request has been rejected.Fix
Add a small checked helper:
It rejects debug block sizes that cannot be represented after adding header/footer overhead. The helper is used before:
GetMemallocates a debug blockReallocMemchecks whether a debug block can be resized in placeReallocMemallocates a replacement debug blockThe change is intentionally limited to debug-mode paths.
Reproducer
I kept the patch minimal and did not add a new test directory because the repository does not currently appear to have one. I can add this reproducer wherever you prefer.
Standalone reproducer:
Validation
Tested locally with RAD Studio 37.0:
dcc64: build passed, reproducer passedDCC32: build passed, reproducer passedExpected successful output:
Risk
Low. Normal-mode allocator paths are unchanged. The fix only adds validation before debug-mode code calculates actual debug block sizes or mutates debug metadata for a resize.