fix: use defer for file handle cleanup in readDirNames

Previously, the file handle was closed explicitly after calling
Readdirnames(-1). This approach has two issues:

1. If Readdirnames() panics, the file handle is never closed (resource leak)
2. The explicit Close() doesn't follow Go best practices

Changed to use 'defer f.Close()' immediately after successful Open(),
ensuring the file handle is always closed when the function exits,
regardless of the execution path.

This follows the standard Go idiom for resource cleanup and prevents
potential file handle leaks in edge cases.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Shalabh Gupta 2026-03-09 12:25:28 +05:30
parent 7760ccefba
commit e4ca72be0a

View file

@ -406,9 +406,9 @@ func readDirNames(path string) ([]string, error) {
if err != nil {
return nil, err
}
defer f.Close()
paths, err := f.Readdirnames(-1)
f.Close()
if err != nil {
return nil, err
}