Skip to content

Go: Improve precision of go/unhandled-writable-file-close#21940

Open
owen-mc wants to merge 9 commits into
github:mainfrom
owen-mc:go/unhandled-writable-file-close
Open

Go: Improve precision of go/unhandled-writable-file-close#21940
owen-mc wants to merge 9 commits into
github:mainfrom
owen-mc:go/unhandled-writable-file-close

Conversation

@owen-mc
Copy link
Copy Markdown
Contributor

@owen-mc owen-mc commented Jun 4, 2026

The query go/unhandled-writable-file-close ("Writable file handle closed without error handling") now produces fewer false positives. A deferred call to Close that is preceded on every execution path by a handled call to Sync on the same file handle is no longer flagged.

owen-mc added 5 commits June 4, 2026 12:21
There are paths to the exit of the function which go through the defer
statement and paths which don't, so we add an optional call to the
deferred function. This causes FPs in the query as it stands.
Correct the doc for unhandledCall (it also matches expression statements where the result is discarded) and remove a stale commented-out line in isWritableFileHandle.
A deferred Close runs at function exit, but the CFG splices it in at the exit node where it can be reached along paths that never execute Sync. The previous dominance check therefore produced a false positive when a statement followed the if-block that registered the defer (e.g. deferredCloseWithSync2). For deferred closes, require instead that a handled Sync post-dominates the point where the defer is registered, which guarantees Sync runs before Close on every path on which Close is registered. Non-deferred closes keep the existing dominance check.
@owen-mc owen-mc requested a review from a team as a code owner June 4, 2026 12:31
Copilot AI review requested due to automatic review settings June 4, 2026 12:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 improves the Go query go/unhandled-writable-file-close to reduce false positives when a deferred Close is guaranteed (on all relevant paths) to be preceded by a handled Sync on the same file handle.

Changes:

  • Refines the query logic to treat deferred Close differently from non-deferred Close when reasoning about whether a handled Sync precedes it.
  • Extends the query test suite with an additional “previously spurious” scenario and updates expected results accordingly.
  • Adds a Go change note documenting the precision improvement.
Show a summary per file
File Description
go/ql/src/InconsistentCode/UnhandledCloseWritableHandle.ql Updates query/control-flow reasoning to reduce false positives for deferred closes preceded by handled syncs.
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/tests.go Adds a new regression scenario marked as previously spurious.
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/UnhandledCloseWritableHandle.expected Updates expected results and records fixed spurious expectations.
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/DebugDefer.ql Adds a CFG-dumping debug query (currently missing a matching expected artifact).
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/DebugDefer2.ql Adds a CFG-dumping query variant for the same functions.
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/DebugDefer2.expected Stores expected output for the CFG dump.
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/DebugDefer3.ql Adds another CFG-dumping query variant (near-duplicate of DebugDefer2).
go/ql/test/query-tests/InconsistentCode/UnhandledCloseWritableHandle/DebugDefer3.expected Stores expected output for the CFG dump.
go/ql/src/change-notes/2026-06-04-unhandled-writable-file-close.md Documents the precision improvement in change notes.

Copilot's findings

  • Files reviewed: 9/9 changed files
  • Comments generated: 3

@owen-mc owen-mc force-pushed the go/unhandled-writable-file-close branch from 94b75a5 to 14e3ee2 Compare June 4, 2026 12:40
@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Jun 4, 2026

DCA showed no significant performance impact and no result changes.

@owen-mc owen-mc requested a review from a team June 4, 2026 15:50
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.

2 participants