fix(os): preserve close errors in CopyFile - #3458
zjubiology wants to merge 1 commit into
Conversation
Signed-off-by: zjubiology <zjubiology@outlook.com>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review. 📝 WalkthroughWalkthrough
ChangesCopyFile error handling
Priority: ⬇️ Low Estimated code review effort: 1 (Trivial) | ~3 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to CopyFile now reports close failures without replacing earlier errors. No actionable merge-blocking risk remains. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change can report a close failure after copying has completed, which may affect how callers decide to retry. No new filesystem access or privilege path is evident, but behavior of callers outside this repository is unknown. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@tac0turtle Could you please take a look when you have a chance? Thanks! |
Overview
CopyFilehas two deferred closures that are meant to return aClose()errorwhen the copy itself succeeded:
This can't work with an unnamed result.
return errcommits the value to theresult slot before the defers run, so
err = cerronly mutates the localvariable and is discarded.
Fix is to name the result:
The existing defers now behave as written. Function body unchanged.
Notes:
info, err := srcfile.Stat()and friends assign rather than shadow, since anamed result lives in the same scope as the function body.
nonamedreturns/nakedretaren't enabled in.golangci.yml.Close()error on the read-only source will now also surface. I kept theoriginal semantics since the comments state that intent, but happy to narrow
this to the destination handle if you'd prefer.
No test added: forcing
Close()to fail whileio.Copysucceeds isn'tportably reproducible without threading
io.WriteCloserthrough the internals.Happy to do that in a follow-up if you want the path covered.
gofmt,go vetandgo test ./pkg/os/...pass.Summary by CodeRabbit