Skip to content

Fix validatePath panic when the config path cannot be stat'ed - #335

Open
akshat-lakhera wants to merge 1 commit into
google:mainfrom
akshat-lakhera:fix/334-validatepath-panic
Open

akshat-lakhera wants to merge 1 commit into
google:mainfrom
akshat-lakhera:fix/334-validatepath-panic

Conversation

@akshat-lakhera

Copy link
Copy Markdown

Fixes #334

When os.Stat on the -conf path failed with anything other than a not-exist error, validatePath went on to call info.IsDir() on a nil FileInfo and panicked. For example, -conf file.txt/.yamlfmt fails with ENOTDIR on Linux. The IsDir check also sat inside the error branch, so the "config path is a directory" error was never returned, and a directory passed to -conf failed later with "read d: is a directory".

The stat error is now returned wrapped in configPathError, and the IsDir check runs after a successful stat. With the fix, -conf file.txt/.yamlfmt prints the stat error and exits 1, and -conf d prints "config path d is a directory".

I added cmd/yamlfmt/config_test.go for the existing file, missing file, directory and under-a-regular-file cases. On Linux the directory and under-a-file tests fail before the fix, and the under-a-file one hits the nil pointer panic. One thing to know: the yamlfmt line in .gitignore also matches the cmd/yamlfmt directory, so I had to git add -f the new test file.

Signed-off-by: Akshat Lakhera <akshatlnct2024@gmail.com>
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.

Panic in validatePath when the -conf path can't be stat'ed

1 participant