Repository navigation
Fix validatePath panic when the config path cannot be stat'ed - #335
Open
akshat-lakhera wants to merge 1 commit into
Open
akshat-lakhera wants to merge 1 commit into
akshat-lakhera wants to merge 1 commit into
Conversation
Signed-off-by: Akshat Lakhera <akshatlnct2024@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.