Skip to content

Fix for Writable file handle closed without error handling#5

Merged
alxxjohn merged 1 commit into
mainfrom
finding-autofix-2
Jun 16, 2026
Merged

Fix for Writable file handle closed without error handling#5
alxxjohn merged 1 commit into
mainfrom
finding-autofix-2

Conversation

@alxxjohn

Copy link
Copy Markdown
Contributor

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) in copyFile, then in defer set err = closeErr only if err == nil.

In internal/codeguard/runner/support/diff_command.go, update copyFile:

  • Change signature to func copyFile(...) (err error).
  • Keep src close as best-effort (read handle; not the CodeQL finding).
  • Replace defer dst.Close() with:
    • defer func() { if closeErr := dst.Close(); err == nil && closeErr != nil { err = closeErr } }()
  • Keep the final io.Copy call assigning to err and returning it.
    This ensures close failures on the writable destination are not silently dropped.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@alxxjohn
alxxjohn marked this pull request as ready for review June 16, 2026 20:52
@alxxjohn
alxxjohn merged commit 6582b21 into main Jun 16, 2026
13 checks passed
@alxxjohn
alxxjohn deleted the finding-autofix-2 branch June 16, 2026 21:44
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