From 704275b7c664fffb16af6802f8c6189706f916f9 Mon Sep 17 00:00:00 2001 From: Zengxian Ding Date: Thu, 14 Jul 2022 16:47:06 +0800 Subject: [PATCH] tools/check: add logging and path sanitization (#36083) close pingcap/tidb#36082 --- tools/check/ut.go | 45 +++++++++++++++++++++++--------------------- tools/check/xprog.go | 9 ++++++--- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/tools/check/ut.go b/tools/check/ut.go index 45b969360d245..abef09e5b155d 100644 --- a/tools/check/ut.go +++ b/tools/check/ut.go @@ -20,10 +20,12 @@ import ( "encoding/xml" "fmt" "io" + "log" "math/rand" "os" "os/exec" "path" + "path/filepath" "regexp" "runtime" "sort" @@ -97,7 +99,7 @@ var workDir string func cmdList(args ...string) bool { pkgs, err := listPackages() if err != nil { - fmt.Println("list package error", err) + log.Println("list package error", err) return false } @@ -120,12 +122,12 @@ func cmdList(args ...string) bool { err := buildTestBinary(pkg) if err != nil { - fmt.Println("build package error", pkg, err) + log.Println("build package error", pkg, err) return false } exist, err := testBinaryExist(pkg) if err != nil { - fmt.Println("check test binary existence error", err) + log.Println("check test binary existence error", err) return false } if !exist { @@ -135,7 +137,7 @@ func cmdList(args ...string) bool { res, err := listTestCases(pkg, nil) if err != nil { - fmt.Println("list test cases for package error", err) + log.Println("list test cases for package error", err) return false } @@ -157,7 +159,7 @@ func cmdList(args ...string) bool { func cmdBuild(args ...string) bool { pkgs, err := listPackages() if err != nil { - fmt.Println("list package error", err) + log.Println("list package error", err) return false } @@ -165,7 +167,7 @@ func cmdBuild(args ...string) bool { if len(args) == 0 { err := buildTestBinaryMulti(pkgs) if err != nil { - fmt.Println("build package error", pkgs, err) + log.Println("build package error", pkgs, err) return false } return true @@ -176,7 +178,7 @@ func cmdBuild(args ...string) bool { pkg := args[0] err := buildTestBinary(pkg) if err != nil { - fmt.Println("build package error", pkg, err) + log.Println("build package error", pkg, err) return false } } @@ -196,14 +198,14 @@ func cmdRun(args ...string) bool { if len(args) == 0 { err := buildTestBinaryMulti(pkgs) if err != nil { - fmt.Println("build package error", pkgs, err) + log.Println("build package error", pkgs, err) return false } for _, pkg := range pkgs { exist, err := testBinaryExist(pkg) if err != nil { - fmt.Println("check test binary existence error", err) + log.Println("check test binary existence error", err) return false } if !exist { @@ -213,7 +215,7 @@ func cmdRun(args ...string) bool { tasks, err = listTestCases(pkg, tasks) if err != nil { - fmt.Println("list test cases error", err) + log.Println("list test cases error", err) return false } } @@ -224,12 +226,12 @@ func cmdRun(args ...string) bool { pkg := args[0] err := buildTestBinary(pkg) if err != nil { - fmt.Println("build package error", pkg, err) + log.Println("build package error", pkg, err) return false } exist, err := testBinaryExist(pkg) if err != nil { - fmt.Println("check test binary existence error", err) + log.Println("check test binary existence error", err) return false } @@ -239,7 +241,7 @@ func cmdRun(args ...string) bool { } tasks, err = listTestCases(pkg, tasks) if err != nil { - fmt.Println("list test cases error", err) + log.Println("list test cases error", err) return false } } @@ -249,12 +251,12 @@ func cmdRun(args ...string) bool { pkg := args[0] err := buildTestBinary(pkg) if err != nil { - fmt.Println("build package error", pkg, err) + log.Println("build package error", pkg, err) return false } exist, err := testBinaryExist(pkg) if err != nil { - fmt.Println("check test binary existence error", err) + log.Println("check test binary existence error", err) return false } if !exist { @@ -264,12 +266,12 @@ func cmdRun(args ...string) bool { tasks, err = listTestCases(pkg, tasks) if err != nil { - fmt.Println("list test cases error", err) + log.Println("list test cases error", err) return false } tasks, err = filterTestCases(tasks, args[1]) if err != nil { - fmt.Println("filter test cases error", err) + log.Println("filter test cases error", err) return false } } @@ -277,7 +279,7 @@ func cmdRun(args ...string) bool { if except != "" { list, err := parseCaseListFromFile(except) if err != nil { - fmt.Println("parse --except file error", err) + log.Println("parse --except file error", err) return false } tmp := tasks[:0] @@ -292,7 +294,7 @@ func cmdRun(args ...string) bool { if only != "" { list, err := parseCaseListFromFile(only) if err != nil { - fmt.Println("parse --only file error", err) + log.Println("parse --only file error", err) return false } tmp := tasks[:0] @@ -349,7 +351,7 @@ func cmdRun(args ...string) bool { } func parseCaseListFromFile(fileName string) (map[string]struct{}, error) { - f, err := os.Open(fileName) + f, err := os.Open(filepath.Clean(fileName)) if err != nil { return nil, withTrace(err) } @@ -624,7 +626,7 @@ func (b blocksByStart) Less(i, j int) bool { func listTestCases(pkg string, tasks []task) ([]task, error) { newCases, err := listNewTestCases(pkg) if err != nil { - fmt.Println("list test case error", pkg, err) + log.Println("list test case error", pkg, err) return nil, withTrace(err) } for _, c := range newCases { @@ -818,6 +820,7 @@ func (n *numa) testCommand(pkg string, fn string) *exec.Cmd { // session.test -test.run TestClusteredPrefixColum args = append(args, "-test.run", "^"+fn+"$") + return exec.Command(exe, args...) } diff --git a/tools/check/xprog.go b/tools/check/xprog.go index 23b8ebffe141e..e31bd0d52f354 100644 --- a/tools/check/xprog.go +++ b/tools/check/xprog.go @@ -34,7 +34,7 @@ func main() { cwd := os.Args[0] cwd = cwd[:len(cwd)-len("tools/bin/xprog")] - testBinaryPath := os.Args[1] + testBinaryPath := filepath.Clean(os.Args[1]) dir, _ := filepath.Split(testBinaryPath) // Extract the package info from /tmp/go-build2662369829/b1382/importcfg.link @@ -51,7 +51,7 @@ func main() { _, file := filepath.Split(pkg) // The path of the destination file looks like $CWD/util/topsql/topsql.test.bin - newName := filepath.Join(cwd, pkg, file+".test.bin") + newName := filepath.Clean(filepath.Join(cwd, pkg, file+".test.bin")) if err1 := os.Rename(testBinaryPath, newName); err1 != nil { // Rename fail, handle error like "invalid cross-device linkcd tools/check" @@ -64,7 +64,7 @@ func main() { func getPackageInfo(dir string) string { // Read the /tmp/go-build2662369829/b1382/importcfg.link file to get the package information - f, err := os.Open(filepath.Join(dir, "importcfg.link")) + f, err := os.Open(filepath.Join(filepath.Clean(dir), "importcfg.link")) if err != nil { os.Exit(-1) } @@ -84,6 +84,9 @@ func getPackageInfo(dir string) string { } func MoveFile(sourcePath, destPath string) error { + filepath.Clean(sourcePath) + filepath.Clean(destPath) + inputFile, err := os.Open(sourcePath) if err != nil { return fmt.Errorf("Couldn't open source file: %s", err)