mirror of
https://github.com/securego/gosec.git
synced 2024-12-24 11:35:52 +00:00
Check input files and handle panic condition
This commit is contained in:
parent
e2caa921fe
commit
0bd254c2eb
1 changed files with 14 additions and 1 deletions
15
tools.go
15
tools.go
|
@ -19,6 +19,7 @@ import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -64,11 +65,23 @@ func (u *utilities) run(args ...string) {
|
||||||
|
|
||||||
func dumpAst(files ...string) {
|
func dumpAst(files ...string) {
|
||||||
for _, arg := range files {
|
for _, arg := range files {
|
||||||
|
// Ensure file exists and not a directory
|
||||||
|
st, e := os.Stat(arg)
|
||||||
|
if e != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", arg, e)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if st.IsDir() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", arg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Create the AST by parsing src.
|
// Create the AST by parsing src.
|
||||||
fset := token.NewFileSet() // positions are relative to fset
|
fset := token.NewFileSet() // positions are relative to fset
|
||||||
f, err := parser.ParseFile(fset, arg, nil, 0)
|
f, err := parser.ParseFile(fset, arg, nil, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fmt.Fprintf(os.Stderr, "Unable to parse file %s\n", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the AST.
|
// Print the AST.
|
||||||
|
|
Loading…
Reference in a new issue