Add PatchSizeHelper and guard against fast-buffer allocations#8
Open
Cryotechnic wants to merge 1 commit into
Open
Add PatchSizeHelper and guard against fast-buffer allocations#8Cryotechnic wants to merge 1 commit into
Cryotechnic wants to merge 1 commit into
Conversation
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; | ||
| } |
There was a problem hiding this comment.
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~190SharpHDiffPatch.Core/Patch/PatchSingle.cs:29~51
Did we get this right? 👍 / 👎 to inform future reviews.
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.
PatchSizeHelperto centralize int32-safety checks and fast-buffer eligibility (CanUseFastBuffer,ToCheckedInt32).PatchCoreFastBuffernow 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.PatchDirandPatchSinglewere refactored to create the appropriate IPatchCore via CreatePatchCore, using PatchSizeHelper to decide between fast-buffer and streaming cores and to log when falling back.