Skip to content

Add PatchSizeHelper and guard against fast-buffer allocations#8

Open
Cryotechnic wants to merge 1 commit into
mainfrom
fix/fastbuffer-int32-overflow-fallback
Open

Add PatchSizeHelper and guard against fast-buffer allocations#8
Cryotechnic wants to merge 1 commit into
mainfrom
fix/fastbuffer-int32-overflow-fallback

Conversation

@Cryotechnic

Copy link
Copy Markdown
Member
  • Introduce PatchSizeHelper to centralize int32-safety checks and fast-buffer eligibility (CanUseFastBuffer, ToCheckedInt32).
  • PatchCoreFastBuffer now validates sizes and uses checked conversions when renting/allocating buffers and reading streams, and falls back to the streaming patch core if requirements are exceeded.
  • PatchDir and PatchSingle were refactored to create the appropriate IPatchCore via CreatePatchCore, using PatchSizeHelper to decide between fast-buffer and streaming cores and to log when falling back.
  • Adjusted memory rounding logic and cover buffer sizing to avoid int overflows.

@Cryotechnic Cryotechnic requested a review from neon-nyan June 14, 2026 00:21
Comment on lines +39 to +44
if (!PatchSizeHelper.CanUseFastBuffer(headerInfo))
{
HDiffPatch.Event.PushLog("[PatchCoreFastBuffer::UncoverBufferClipsStream] Fast buffer requirements exceeded; delegating to streaming patch core.", Verbosity.Info);
_core.UncoverBufferClipsStream(clips, inputStream, outputStream, headerInfo);
return;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: Streams are disposed twice, once in the core patching logic and again in the calling methods, which will cause an ObjectDisposedException.
Severity: HIGH

Suggested Fix

The responsibility for disposing streams should be handled in only one place. Remove the Dispose() calls for clips, inputStream, and outputStream from within PatchCore.WriteCoverStreamToOutput and PatchCoreFastBuffer.WriteCoverStreamToOutputFast. Let the calling methods in PatchDir and PatchSingle manage the lifecycle of the streams they create.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: SharpHDiffPatch.Core/Patch/PatchCoreFastBuffer.cs#L39-L44

Potential issue: The `UncoverBufferClipsStream` method, whether using the fast path or
the standard implementation, disposes the `clips`, `inputStream`, and `outputStream`
objects. The calling methods, `PatchDir.StartPatchRoutine` and
`PatchSingle.StartPatchRoutine`, also dispose these same streams in their `finally`
blocks. This guarantees that `Dispose()` will be called twice on the same stream objects
in all execution paths. This will cause an `ObjectDisposedException` when the second
disposal is attempted, leading to a failure in the patching operation.

Also affects:

  • SharpHDiffPatch.Core/Patch/PatchDir.cs:169~190
  • SharpHDiffPatch.Core/Patch/PatchSingle.cs:29~51

Did we get this right? 👍 / 👎 to inform future reviews.

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.

1 participant