Skip to content

fix: proper os.Stat error handling in fileExists (avoid nil deref). Fix #9#10

Merged
icy merged 4 commits intomasterfrom
fix/file-exists-nil-check
Nov 12, 2025
Merged

fix: proper os.Stat error handling in fileExists (avoid nil deref). Fix #9#10
icy merged 4 commits intomasterfrom
fix/file-exists-nil-check

Conversation

@icy
Copy link
Owner

@icy icy commented Oct 27, 2025

Contents below generated by Copilot (gpt-5-mini)

  1. The MR is completely created by AI. Test cases fixed by @icy
  2. Problem: fileExists previously ignored os.Stat errors except for os.IsNotExist(err) and then dereferenced info.IsDir() even when info was nil, which causes a nil-pointer dereference when os.Stat returns a non-nil error that is not ErrNotExist (permission errors, transient FS errors, etc.).
  3. Fix: fileExists now returns (bool, error). For any os.Stat error (including file-not-exist) the function returns false and a single combined error message ("file non-exist or error: ..."). The caller in main checks the returned error first and fails with a clear message. This avoids dereferencing a nil FileInfo and makes the behavior explicit.
  4. Rationale: combining all os.Stat errors into a single case matches the user's request ("For any error, return file non-exist or error (combine all error into a single case)"), simplifies caller logic, and prevents runtime panics.
  5. Tests / next steps: we recommend adding unit tests that simulate os.Stat returning ErrNotExist and permission errors to prevent regressions. Optionally change fileExists to return richer typed errors if finer-grained handling is desired later.

@icy icy merged commit fe51c86 into master Nov 12, 2025
4 checks passed
@icy icy deleted the fix/file-exists-nil-check branch November 12, 2025 08:36
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