Fix for Writable file handle closed without error handling#5
Merged
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
alxxjohn
marked this pull request as ready for review
June 16, 2026 20:52
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.
To fix this safely without changing intended behavior, replace the bare
defer dst.Close()with a deferred closure that captures and propagates any close error when no earlier error is already being returned. The cleanest pattern in Go is to use a named return value (err error) incopyFile, then indeferseterr = closeErronly iferr == nil.In
internal/codeguard/runner/support/diff_command.go, updatecopyFile:func copyFile(...) (err error).srcclose as best-effort (read handle; not the CodeQL finding).defer dst.Close()with:defer func() { if closeErr := dst.Close(); err == nil && closeErr != nil { err = closeErr } }()io.Copycall assigning toerrand returning it.This ensures close failures on the writable destination are not silently dropped.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.