Skip to content

Commit

Permalink
sumdb/dirhash: fix a panic when argument is not a directory
Browse files Browse the repository at this point in the history
This patch fixes a case where a path to a non directory
can cause DirHash func to panic.

Fixes: golang/go#57269

Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 committed Dec 12, 2022
1 parent 7c05a44 commit 26b037e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
10 changes: 9 additions & 1 deletion sumdb/dirhash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ func HashDir(dir, prefix string, hash Hash) (string, error) {
func DirFiles(dir, prefix string) ([]string, error) {
var files []string
dir = filepath.Clean(dir)
err := filepath.Walk(dir, func(file string, info os.FileInfo, err error) error {
dirInfo, err := os.Stat(dir)
if err != nil {
return nil, err
}
if !dirInfo.IsDir() {
return nil, fmt.Errorf("invalid directory path")
}

err = filepath.Walk(dir, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand Down
68 changes: 42 additions & 26 deletions sumdb/dirhash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,47 @@ func TestHashZip(t *testing.T) {
}

func TestDirFiles(t *testing.T) {
dir, err := ioutil.TempDir("", "dirfiles-test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
if err := ioutil.WriteFile(filepath.Join(dir, "xyz"), []byte("data for xyz"), 0666); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "abc"), []byte("data for abc"), 0666); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(dir, "subdir"), 0777); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "subdir", "xyz"), []byte("data for subdir xyz"), 0666); err != nil {
t.Fatal(err)
}
prefix := "foo/bar@v2.3.4"
out, err := DirFiles(dir, prefix)
if err != nil {
t.Fatalf("DirFiles: %v", err)
}
for _, file := range out {
if !strings.HasPrefix(file, prefix) {
t.Errorf("Dir file = %s, want prefix %s", file, prefix)
t.Run("valid directory with files", func(t *testing.T) {
dir, err := ioutil.TempDir("", "dirfiles-test-")
if err != nil {
t.Fatal(err)
}
}
defer os.RemoveAll(dir)
if err := ioutil.WriteFile(filepath.Join(dir, "xyz"), []byte("data for xyz"), 0666); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "abc"), []byte("data for abc"), 0666); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(dir, "subdir"), 0777); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "subdir", "xyz"), []byte("data for subdir xyz"), 0666); err != nil {
t.Fatal(err)
}
prefix := "foo/bar@v2.3.4"
out, err := DirFiles(dir, prefix)
if err != nil {
t.Fatalf("DirFiles: %v", err)
}
for _, file := range out {
if !strings.HasPrefix(file, prefix) {
t.Errorf("Dir file = %s, want prefix %s", file, prefix)
}
}
})

t.Run("invalid directory", func(t *testing.T) {
exePath, err := os.Executable()
if err != nil {
t.Fatalf("DirFiles: %v", err)
}
out, err := DirFiles(exePath, "")
if err == nil {
t.Errorf("DirFiles(...) = %s, want %s", err, "invalid directory path")
}
if len(out) > 0 {
t.Errorf("DirFiles(...) = unexpected files %s", out)
}
})
}

0 comments on commit 26b037e

Please sign in to comment.