From e8d48c0ae0740a2aab951e462224ec6f16d67e53 Mon Sep 17 00:00:00 2001 From: guzenok Date: Tue, 29 Dec 2020 20:33:22 +1000 Subject: [PATCH 1/9] Context.clonePackage() also copies non-package dirs --- go-fuzz-build/main.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index b2a2d4d78..14aabc75f 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -665,6 +665,20 @@ func (c *Context) clonePackage(p *packages.Package) { c.copyFile(f, dst) } + // copy subdirs which aren't packages + dir := filepath.Dir(p.GoFiles[0]) + subdirs, err := ioutil.ReadDir(dir) + if err != nil { + c.failf("failed to scan dir '%v': %v", dir, err) + } + for _, d := range subdirs { + if d.IsDir() { + src := filepath.Join(dir, d.Name()) + dst := filepath.Join(newDir, d.Name()) + c.copyNotPkgDir(src, dst) + } + } + // TODO: do we need to look for and copy go.mod? } @@ -753,6 +767,29 @@ func (c *Context) copyDir(dir, newDir string) { } } +func (c *Context) copyNotPkgDir(dir, newDir string) { + files, err := ioutil.ReadDir(dir) + if err != nil { + c.failf("failed to scan dir '%v': %v", dir, err) + } + for _, f := range files { + if strings.HasSuffix(f.Name(), ".go") { + // it is Pkg dir + return + } + } + c.mkdirAll(newDir) + for _, f := range files { + src := filepath.Join(dir, f.Name()) + dst := filepath.Join(newDir, f.Name()) + if f.IsDir() { + c.copyNotPkgDir(src, dst) + continue + } + c.copyFile(src, dst) + } +} + func (c *Context) copyFile(src, dst string) { r, err := os.Open(src) if err != nil { From 73feda7e13bd5db103e8414aa68a36af1902ec23 Mon Sep 17 00:00:00 2001 From: guzenok Date: Sun, 3 Jan 2021 20:40:09 +1000 Subject: [PATCH 2/9] packageDir() returns local directory with package source files --- go-fuzz-build/main.go | 4 ++-- go-fuzz-build/pkgs.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 go-fuzz-build/pkgs.go diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index 14aabc75f..be1c2af65 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -665,8 +665,8 @@ func (c *Context) clonePackage(p *packages.Package) { c.copyFile(f, dst) } - // copy subdirs which aren't packages - dir := filepath.Dir(p.GoFiles[0]) + dir := packageDir(p) + // Сopy subdirs which aren't packages. subdirs, err := ioutil.ReadDir(dir) if err != nil { c.failf("failed to scan dir '%v': %v", dir, err) diff --git a/go-fuzz-build/pkgs.go b/go-fuzz-build/pkgs.go new file mode 100644 index 000000000..eb2b226fa --- /dev/null +++ b/go-fuzz-build/pkgs.go @@ -0,0 +1,17 @@ +// Copyright 2015 go-fuzz project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package main + +import ( + "path/filepath" + + "golang.org/x/tools/go/packages" +) + +// packageDir returns local directory with package source files. +func packageDir(p *packages.Package) string { + // Go-package contains at least one go-file, so GoFiles is not empty without fail. + dir := filepath.Dir(p.GoFiles[0]) + return dir +} From 6bbc7c97f9254a02d19b8098b43c71e9f07bbf5f Mon Sep 17 00:00:00 2001 From: guzenok Date: Sun, 3 Jan 2021 22:01:52 +1000 Subject: [PATCH 3/9] isPackage() checks if dir contains go source files --- go-fuzz-build/main.go | 8 +++----- go-fuzz-build/pkgs.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index be1c2af65..d6cf2fba2 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -772,11 +772,9 @@ func (c *Context) copyNotPkgDir(dir, newDir string) { if err != nil { c.failf("failed to scan dir '%v': %v", dir, err) } - for _, f := range files { - if strings.HasSuffix(f.Name(), ".go") { - // it is Pkg dir - return - } + if isPackage(files) { + // Don't copy go-package dir. + return } c.mkdirAll(newDir) for _, f := range files { diff --git a/go-fuzz-build/pkgs.go b/go-fuzz-build/pkgs.go index eb2b226fa..71472bfd5 100644 --- a/go-fuzz-build/pkgs.go +++ b/go-fuzz-build/pkgs.go @@ -4,7 +4,9 @@ package main import ( + "os" "path/filepath" + "strings" "golang.org/x/tools/go/packages" ) @@ -15,3 +17,13 @@ func packageDir(p *packages.Package) string { dir := filepath.Dir(p.GoFiles[0]) return dir } + +// isPackage checks if dir contains go source files. +func isPackage(files []os.FileInfo) bool { + for _, f := range files { + if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") { + return true + } + } + return false +} From 6c0ebd4e0555b912de34c02871e1e12024b2e870 Mon Sep 17 00:00:00 2001 From: guzenok Date: Sun, 3 Jan 2021 22:47:46 +1000 Subject: [PATCH 4/9] style fix --- go-fuzz-build/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index d6cf2fba2..51045128f 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -782,9 +782,9 @@ func (c *Context) copyNotPkgDir(dir, newDir string) { dst := filepath.Join(newDir, f.Name()) if f.IsDir() { c.copyNotPkgDir(src, dst) - continue + } else { + c.copyFile(src, dst) } - c.copyFile(src, dst) } } From c56131d23be44412ee834cfa07347935a768dec1 Mon Sep 17 00:00:00 2001 From: guzenok Date: Sun, 3 Jan 2021 23:28:23 +1000 Subject: [PATCH 5/9] join Context.copyNotPkgDir() and copyDir() --- go-fuzz-build/main.go | 29 +++++++---------------------- go-fuzz-build/pkgs.go | 13 ++++++++----- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index 51045128f..bdc8ecae3 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -675,7 +675,7 @@ func (c *Context) clonePackage(p *packages.Package) { if d.IsDir() { src := filepath.Join(dir, d.Name()) dst := filepath.Join(newDir, d.Name()) - c.copyNotPkgDir(src, dst) + c.copyDir(src, dst, isNotPackage) } } @@ -750,38 +750,23 @@ func (c *Context) instrumentPackages(blocks *[]CoverBlock, sonar *[]CoverBlock) packages.Visit(c.pkgs, nil, visit) } -func (c *Context) copyDir(dir, newDir string) { +func (c *Context) copyDir(dir, newDir string, filters ...func([]os.FileInfo) bool) { files, err := ioutil.ReadDir(dir) if err != nil { c.failf("failed to scan dir '%v': %v", dir, err) } - c.mkdirAll(newDir) - for _, f := range files { - src := filepath.Join(dir, f.Name()) - dst := filepath.Join(newDir, f.Name()) - if f.IsDir() { - c.copyDir(src, dst) - } else { - c.copyFile(src, dst) + for _, filtered := range filters { + if !filtered(files) { + // Don't copy filtered dir. + return } } -} - -func (c *Context) copyNotPkgDir(dir, newDir string) { - files, err := ioutil.ReadDir(dir) - if err != nil { - c.failf("failed to scan dir '%v': %v", dir, err) - } - if isPackage(files) { - // Don't copy go-package dir. - return - } c.mkdirAll(newDir) for _, f := range files { src := filepath.Join(dir, f.Name()) dst := filepath.Join(newDir, f.Name()) if f.IsDir() { - c.copyNotPkgDir(src, dst) + c.copyDir(src, dst, filters...) } else { c.copyFile(src, dst) } diff --git a/go-fuzz-build/pkgs.go b/go-fuzz-build/pkgs.go index 71472bfd5..d291ec8c5 100644 --- a/go-fuzz-build/pkgs.go +++ b/go-fuzz-build/pkgs.go @@ -18,12 +18,15 @@ func packageDir(p *packages.Package) string { return dir } -// isPackage checks if dir contains go source files. -func isPackage(files []os.FileInfo) bool { +// isNotPackage checks if dir contains go source files. +func isNotPackage(files []os.FileInfo) bool { for _, f := range files { - if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") { - return true + if f.IsDir() { + continue + } + if strings.HasSuffix(f.Name(), ".go") { + return false } } - return false + return true } From 5ca9f07540b9de2985126d0de468df6f67335f47 Mon Sep 17 00:00:00 2001 From: guzenok Date: Tue, 29 Jun 2021 14:16:16 +1000 Subject: [PATCH 6/9] style fix "Using filepath.Dir(p.GoFiles[0]) inline, the way it was, is better. The comment that p.GoFiles is non-empty is useful and should stay, but it could be shortened." --- go-fuzz-build/main.go | 3 ++- go-fuzz-build/pkgs.go | 10 ---------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index bdc8ecae3..920736ceb 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -665,7 +665,8 @@ func (c *Context) clonePackage(p *packages.Package) { c.copyFile(f, dst) } - dir := packageDir(p) + // p.GoFiles is always non-empty. + dir := filepath.Dir(p.GoFiles[0]) // Сopy subdirs which aren't packages. subdirs, err := ioutil.ReadDir(dir) if err != nil { diff --git a/go-fuzz-build/pkgs.go b/go-fuzz-build/pkgs.go index d291ec8c5..6d6032518 100644 --- a/go-fuzz-build/pkgs.go +++ b/go-fuzz-build/pkgs.go @@ -5,19 +5,9 @@ package main import ( "os" - "path/filepath" "strings" - - "golang.org/x/tools/go/packages" ) -// packageDir returns local directory with package source files. -func packageDir(p *packages.Package) string { - // Go-package contains at least one go-file, so GoFiles is not empty without fail. - dir := filepath.Dir(p.GoFiles[0]) - return dir -} - // isNotPackage checks if dir contains go source files. func isNotPackage(files []os.FileInfo) bool { for _, f := range files { From fe5a7730ea16d0d8703ec317c31e54729382427c Mon Sep 17 00:00:00 2001 From: guzenok Date: Tue, 29 Jun 2021 14:23:51 +1000 Subject: [PATCH 7/9] style fix "place isNotPackage() in main.go along with the rest, near where they are used" --- go-fuzz-build/main.go | 13 +++++++++++++ go-fuzz-build/pkgs.go | 22 ---------------------- 2 files changed, 13 insertions(+), 22 deletions(-) delete mode 100644 go-fuzz-build/pkgs.go diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index 920736ceb..75d401052 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -683,6 +683,19 @@ func (c *Context) clonePackage(p *packages.Package) { // TODO: do we need to look for and copy go.mod? } +// isNotPackage checks if dir contains go source files. +func isNotPackage(files []os.FileInfo) bool { + for _, f := range files { + if f.IsDir() { + continue + } + if strings.HasSuffix(f.Name(), ".go") { + return false + } + } + return true +} + // packageNamed extracts the package listed in path. func (c *Context) packageNamed(path string) (pkgs *packages.Package) { all := c.packagesNamed(path) diff --git a/go-fuzz-build/pkgs.go b/go-fuzz-build/pkgs.go deleted file mode 100644 index 6d6032518..000000000 --- a/go-fuzz-build/pkgs.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2015 go-fuzz project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -package main - -import ( - "os" - "strings" -) - -// isNotPackage checks if dir contains go source files. -func isNotPackage(files []os.FileInfo) bool { - for _, f := range files { - if f.IsDir() { - continue - } - if strings.HasSuffix(f.Name(), ".go") { - return false - } - } - return true -} From ce28dfd0f14418564eeedfdfc8eac9b1ff40d7e2 Mon Sep 17 00:00:00 2001 From: guzenok Date: Tue, 29 Jun 2021 14:31:09 +1000 Subject: [PATCH 8/9] style fix "Negatives are harder to read at call sites. I'd prefer isPackage" --- go-fuzz-build/main.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index 75d401052..f97167dbd 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -676,24 +676,24 @@ func (c *Context) clonePackage(p *packages.Package) { if d.IsDir() { src := filepath.Join(dir, d.Name()) dst := filepath.Join(newDir, d.Name()) - c.copyDir(src, dst, isNotPackage) + c.copyDir(src, dst, isPackage) } } // TODO: do we need to look for and copy go.mod? } -// isNotPackage checks if dir contains go source files. -func isNotPackage(files []os.FileInfo) bool { +// isPackage checks if dir contains go source files. +func isPackage(files []os.FileInfo) bool { for _, f := range files { if f.IsDir() { continue } if strings.HasSuffix(f.Name(), ".go") { - return false + return true } } - return true + return false } // packageNamed extracts the package listed in path. @@ -769,9 +769,8 @@ func (c *Context) copyDir(dir, newDir string, filters ...func([]os.FileInfo) boo if err != nil { c.failf("failed to scan dir '%v': %v", dir, err) } - for _, filtered := range filters { - if !filtered(files) { - // Don't copy filtered dir. + for _, filter := range filters { + if rejected := filter(files); rejected { return } } From a8e90a2a4f880c82fe6c01a3897f24f1daad8450 Mon Sep 17 00:00:00 2001 From: guzenok Date: Tue, 29 Jun 2021 14:34:11 +1000 Subject: [PATCH 9/9] style fix "This comment should read: ..." --- go-fuzz-build/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index f97167dbd..68c2d53a4 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -683,7 +683,7 @@ func (c *Context) clonePackage(p *packages.Package) { // TODO: do we need to look for and copy go.mod? } -// isPackage checks if dir contains go source files. +// isPackage reports whether dir contains Go source files. func isPackage(files []os.FileInfo) bool { for _, f := range files { if f.IsDir() {