Skip to content

Fix Arraypool rent without try/finally block#129158

Open
alinpahontu2912 wants to merge 1 commit into
dotnet:mainfrom
alinpahontu2912:alin/tar-arraypool-leak-fix
Open

Fix Arraypool rent without try/finally block#129158
alinpahontu2912 wants to merge 1 commit into
dotnet:mainfrom
alinpahontu2912:alin/tar-arraypool-leak-fix

Conversation

@alinpahontu2912

Copy link
Copy Markdown
Member

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens System.Formats.Tar against ArrayPool<byte> buffer leaks by ensuring all rented arrays are returned via try/finally, even when an exception occurs during stream I/O or header processing.

Changes:

  • Wrap ArrayPool<byte>.Shared.Rent(...) usages in try/finally in writer paths so buffers are always returned.
  • Wrap ArrayPool<byte>.Shared.Rent(...) usages in try/finally in reader paths and stream-advancing/copy helpers to prevent leaks on read failures.
  • Ensure temporary pooled buffers used for padding / metadata generation are returned on all exit paths.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.cs Ensures pooled buffers used during async entry writes and final record writes are always returned.
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs Ensures pooled buffers in stream advance/copy helpers are always returned, even on read/write failures.
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs Ensures pooled buffers for async padding writes and extended-attribute stream generation are always returned.
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs Ensures pooled buffers used while reading headers and metadata blocks are always returned on all paths.

@gfoidl

gfoidl commented Jun 9, 2026

Copy link
Copy Markdown
Member

Is this needed with Unsafe code best practices in mind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants