From 6872afaef7d98a12294ad760ba51b34eeefd2a6c Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Sun, 3 May 2020 00:13:52 +0300 Subject: [PATCH] Fix zipFiler crash on dirs with subdirs This happens since nodes for directories don't have their "file" field initialized. Signed-off-by: Tal Einat --- licensedb/filer/filer.go | 6 +++++- licensedb/filer/filer_test.go | 18 ++++++++++++++++++ licensedb/filer/test_data/empty_sub_dir.zip | Bin 0 -> 310 bytes 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 licensedb/filer/test_data/empty_sub_dir.zip diff --git a/licensedb/filer/filer.go b/licensedb/filer/filer.go index 32fbb8f..707368b 100644 --- a/licensedb/filer/filer.go +++ b/licensedb/filer/filer.go @@ -323,9 +323,13 @@ func (filer *zipFiler) ReadDir(path string) ([]File, error) { } result := make([]File, 0, len(node.children)) for name, child := range node.children { + isDir := true + if child.file != nil { + isDir = child.file.FileInfo().IsDir() + } result = append(result, File{ Name: name, - IsDir: child.file.FileInfo().IsDir(), + IsDir: isDir, }) } return result, nil diff --git a/licensedb/filer/filer_test.go b/licensedb/filer/filer_test.go index 2273aff..0db6382 100644 --- a/licensedb/filer/filer_test.go +++ b/licensedb/filer/filer_test.go @@ -79,9 +79,27 @@ func TestZipFiler(t *testing.T) { filer, err := FromZIP("test_data/local.zip") assert.Nil(t, err) testFiler(t, filer) + filer, err = FromZIP("test_data/local2.zip") assert.Nil(t, filer) assert.NotNil(t, err) + + filer, err = FromZIP("test_data/empty_sub_dir.zip") + assert.Nil(t, err) + defer filer.Close() + files, err := filer.ReadDir("") + assert.Nil(t, err) + assert.Len(t, files, 1) + assert.Equal(t, "dir", files[0].Name) + assert.True(t, files[0].IsDir) + files, err = filer.ReadDir("dir") + assert.Nil(t, err) + assert.Len(t, files, 1) + assert.Equal(t, "sub_dir", files[0].Name) + assert.True(t, files[0].IsDir) + files, err = filer.ReadDir("dir/sub_dir") + assert.Nil(t, err) + assert.Len(t, files, 0) } func TestNestedFiler(t *testing.T) { diff --git a/licensedb/filer/test_data/empty_sub_dir.zip b/licensedb/filer/test_data/empty_sub_dir.zip new file mode 100644 index 0000000000000000000000000000000000000000..2fbad9e10d7a6453c567e3596e12ed503d9a2cdb GIT binary patch literal 310 zcmWIWW@h1H00AA2#Q|Ujlwe_yVMxg=(hm*cWMJO>c5NI8msW5yFtU7QWME(s0jdjt z>y~Cm)y)IdU0j+J4>x7$o3(KujA05RlN>WHS4cqJBf#+15yXU;#tLy09+NO#glv)> a)Fg!afF@#f8!H>gRwf{v1EgC(90mXz=QAPz literal 0 HcmV?d00001