Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmd/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ import (

func checkPathExist(path string, isDir bool) bool {
stat, err := os.Lstat(path) // Note: os.Lstat() will not follow the symbolic link.
isExists := !os.IsNotExist(err)
if err != nil {
return false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes the original nil-dereference risk (the old code would call stat.IsDir() on a nil stat when err was non-nil but not an IsNotExist error). However, it now silently treats all errors — including EACCES/permission denied and EIO — as "path does not exist". Consider adding a comment to make this policy explicit, or handle unexpected errors separately:

if err != nil {
    if os.IsNotExist(err) {
        return false
    }
    // Permission denied or I/O error — treat as absent, or surface with log.Printf
    return false
}

At minimum, a comment like // Treat all errors, including permission denied, as non-existence would make the intent clear.

}
if isDir {
return isExists && stat.IsDir()
return stat.IsDir()
}
return isExists && !stat.IsDir()
return !stat.IsDir()
Comment on lines 41 to +44

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The conditional check can be simplified by directly comparing stat.IsDir() with the isDir boolean parameter. This makes the code more concise and idiomatic.

	return stat.IsDir() == isDir

}

func trimRight(s string) string {
Expand Down
8 changes: 5 additions & 3 deletions cmd/make_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ var mainVersionFile = "env/version.go"

func checkPathExist(path string, isDir bool) bool {
stat, err := os.Stat(path)
isExists := !os.IsNotExist(err)
if err != nil {
return false
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same behavioral change as in cmd/make.go. Note that this copy uses os.Stat (follows symlinks) while the production copy uses os.Lstat (does not follow symlinks), so tests do not cover symlink-specific behaviour. This divergence is pre-existing and not introduced by this PR, but worth noting if symlink robustness ever becomes a concern.

if isDir {
return isExists && stat.IsDir()
return stat.IsDir()
}
return isExists && !stat.IsDir()
return !stat.IsDir()
Comment on lines 33 to +36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The conditional check can be simplified by directly comparing stat.IsDir() with the isDir boolean parameter. This makes the code more concise and idiomatic.

	return stat.IsDir() == isDir

}

func trimRight(s string) string {
Expand Down
Loading