fix(doc): resolve file descriptor leaks in documentation generators#992
fix(doc): resolve file descriptor leaks in documentation generators#992CygnusMaximillian wants to merge 2 commits into
Conversation
Signed-off-by: CygnusMaximillian <dprajjwal11@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #992 +/- ##
=========================================
- Coverage 10.99% 9.34% -1.65%
=========================================
Files 173 321 +148
Lines 8671 16089 +7418
=========================================
+ Hits 953 1504 +551
- Misses 7612 14442 +6830
- Partials 106 143 +37 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bupd
left a comment
There was a problem hiding this comment.
/lgtm
@CygnusMaximillian Thanks for your contribution
There was a problem hiding this comment.
Pull request overview
This PR aims to eliminate file descriptor leaks in the CLI documentation generators by ensuring files created/opened during doc generation are reliably closed on all error paths.
Changes:
- Move
defer f.Close()to immediately afteros.CreateinMarkdownTreeCustomto prevent leaks on write failures. - Replace manual
Close()calls in man page cleanup withdeferto ensure closure on early returns.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
doc/doc.go |
Registers defer f.Close() immediately after os.Create so write/generation failures don’t leak the output file descriptor. |
doc/man-docs/man_doc.go |
Switches to deferred closes during man page read/write cleanup to cover early error returns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -83,8 +83,8 @@ func cleanManPages(docDir string) error { | |||
| if err != nil { | |||
| return err | |||
| } | |||
| defer f.Close() | |||
| content, err := io.ReadAll(f) | |||
Signed-off-by: Prasanth Baskar <bupdprasanth@gmail.com>
Description
This PR fixes a subtle file descriptor leak vulnerability within the CLI documentation generator logic (
doc/doc.goanddoc/man-docs/man_doc.go).Previously,
defer f.Close()was placed after write operations (likeio.WriteString), or handled manually in a way that missed earlyerr != nilreturn paths. If an I/O operation failed mid-way, the function exited prematurely, leaving the file descriptor open and causing potential resource exhaustion.Changes Made
defer f.Close()to execute immediately after checkingos.Createerrors, guaranteeing closure on subsequent write failures.defercalls for robust resource management.Verification Results
I have verified these changes locally:
go build ./...completes successfully.go test ./...passes without regressions.Closes
Closes #991
Screenshots of Tests
also I ran general go test ./... for man_doc.go as there are no dedicated test for it