Fix debug AllocMem zero-fill header lookup - #67
Closed
janrysavy wants to merge 1 commit into
Closed
Conversation
FastMM_DebugGetMem_GetDebugBlock returns a pointer to user data, but FastMM_DebugAllocMem used that pointer as if it were the debug block header when reading StackTraceEntryCount. For medium debug allocations near the medium/large threshold, stale user data could affect the calculated footer size and make AllocMem skip the required zero-fill. Read StackTraceEntryCount from the real debug header at PByte(Result) - CDebugBlockHeaderSize. A standalone reproducer was validated locally with RAD Studio 37.0 for Win32 and Win64 and is kept outside the upstream PR tree.
This was referenced Apr 28, 2026
Owner
|
Hi Jan, thank you very much for the report. I don't know how I missed that. The RTL makes very little use of AllocMem (mostly calling GetMem followed by FillChar), which is probably why it sailed under the radar for so long. I have pushed a fix for it. |
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
We have been long-time users of FastMM and rely on it in our Delphi codebases. Recently we started using GPT-5.5 PRO as an additional code review tool, and it has been very effective at finding subtle bugs in our own code. Based on that experience, we tried the same style of review on FastMM5.
For the issues that look actionable, we would like to send small, reproducible PRs with focused fixes, so you can decide whether they make sense for upstream.
This PR fixes one debug-mode
AllocMemissue where the debug header is read from the wrong address.Problem
FastMM_DebugGetMem_GetDebugBlockreturns a pointer to user data:However,
FastMM_DebugAllocMemused that returned pointer as if it still pointed toTFastMM_DebugBlockHeaderwhen readingStackTraceEntryCount.For medium debug allocations near the medium/large threshold, stale bytes from the user area can affect the calculated debug footer size. That can make
FastMM_DebugAllocMemskip the requiredFillChar, causing debug-modeAllocMemto return non-zeroed memory.Fix
Read
StackTraceEntryCountfrom the real debug header:The change is intentionally minimal and only affects debug-mode
AllocMem.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 changes where debug-mode
AllocMemreads the existing stack trace count before deciding whether explicit zero-fill is required.