Skip to content

Constrain MDF extraction streams to avoid pooled buffer bleed#161

Merged
UlyssesWu merged 4 commits into
masterfrom
copilot/fix-arraypool-overlapping-data
Feb 1, 2026
Merged

Constrain MDF extraction streams to avoid pooled buffer bleed#161
UlyssesWu merged 4 commits into
masterfrom
copilot/fix-arraypool-overlapping-data

Conversation

Copilot AI commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

MDF extraction was writing more bytes than intended because ArrayPool rentals can exceed the requested size, leading to trailing garbage or data overlap in unpacked files. This change ensures only the actual payload length is exposed when writing.

  • Pooled buffer safety
    • Wrap ArrayPool-backed buffers in length-bounded, read-only MemoryStream instances for MDF fast-path extraction.
    • Track the true decompressed size and prevent writing beyond it.

Example (length-bounded stream over pooled buffer):

mdfDecompressedLength = ms.MdfGetOriginalLength();
mdfDecompressed = ArrayPool<byte>.Shared.Rent(mdfDecompressedLength);
MdfShell.ToPsb(ms, mdfDecompressed, bodyContext);
mms = new MemoryStream(mdfDecompressed, 0, mdfDecompressedLength, false);
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] ArrayPool causes garbage and overlapping data at the end of the file</issue_title>
<issue_description>Had an issues with script files, they were unpacking with parts of other scripts or bunch of null bytes. A friend of mine took a look and that's what he found.

At least for this bit, but you probably need to check other instances:

mdfDecompressed = ArrayPool<byte>.Shared.Rent(ms.MdfGetOriginalLength());

A length passed to the Rent is minimumLength, so your actual buffer migth be bigger and later you're writing all avaliable bytes from it
+ sometimes you might get the same array from the pool which will contain data from your previous file

</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 31, 2026 18:59
Co-authored-by: UlyssesWu <4312781+UlyssesWu@users.noreply.github.com>
Co-authored-by: UlyssesWu <4312781+UlyssesWu@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ArrayPool causing garbage and overlapping data Constrain MDF extraction streams to avoid pooled buffer bleed Jan 31, 2026
Copilot AI requested a review from UlyssesWu January 31, 2026 19:03
@UlyssesWu UlyssesWu marked this pull request as ready for review February 1, 2026 09:02
@UlyssesWu UlyssesWu merged commit 2ce1888 into master Feb 1, 2026
1 check passed
@UlyssesWu UlyssesWu deleted the copilot/fix-arraypool-overlapping-data branch February 1, 2026 09:08
@UlyssesWu UlyssesWu removed their request for review February 1, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ArrayPool causes garbage and overlapping data at the end of the file

2 participants