Skip to content

Commit 6ace60b

Browse files
committed
Address unhandled error conditions
Closes #95
1 parent 8f78248 commit 6ace60b

File tree

8 files changed

+69
-10
lines changed

8 files changed

+69
-10
lines changed

core/analyzer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"go/types"
2424
"log"
2525
"os"
26+
"path"
2627
"reflect"
2728
"strings"
2829
)
@@ -222,8 +223,9 @@ func (gas *Analyzer) Visit(n ast.Node) ast.Visitor {
222223
for _, rule := range val {
223224
ret, err := rule.Match(n, &gas.context)
224225
if err != nil {
225-
// will want to give more info than this ...
226-
gas.logger.Println("internal error running rule:", err)
226+
file, line := GetLocation(n, &gas.context)
227+
file = path.Base(file)
228+
gas.logger.Printf("Rule error: %v => %s (%s:%d)\n", reflect.TypeOf(rule), err, file, line)
227229
}
228230
if ret != nil {
229231
gas.Issues = append(gas.Issues, *ret)

core/helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,9 @@ func GetImportPath(name string, ctx *Context) (string, bool) {
212212
}
213213
return "", false
214214
}
215+
216+
// GetLocation returns the filename and line number of an ast.Node
217+
func GetLocation(n ast.Node, ctx *Context) (string, int) {
218+
fobj := ctx.FileSet.File(n.Pos())
219+
return fobj.Name(), fobj.Line(n.Pos())
220+
}

filelist.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package main
1616

1717
import (
18+
"fmt"
19+
"os"
1820
"path/filepath"
1921
"strings"
2022
)
@@ -32,7 +34,10 @@ func newFileList(paths ...string) *filelist {
3234
}
3335

3436
for _, path := range paths {
35-
f.Set(path)
37+
if e := f.Set(path); e != nil {
38+
// #nosec
39+
fmt.Fprintf(os.Stderr, "Unable to add %s to filelist: %s\n", path, e)
40+
}
3641
}
3742
return f
3843
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ func buildConfig(incRules string, excRules string) map[string]interface{} {
117117
return config
118118
}
119119

120+
// #nosec
120121
func usage() {
122+
121123
fmt.Fprintln(os.Stderr, usageText)
122124
fmt.Fprint(os.Stderr, "OPTIONS:\n\n")
123125
flag.PrintDefaults()

rules/fileperms.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ func getConfiguredMode(conf map[string]interface{}, configKey string, defaultMod
3636
case int64:
3737
mode = value.(int64)
3838
case string:
39-
mode, _ = strconv.ParseInt(value.(string), 0, 64)
39+
if m, e := strconv.ParseInt(value.(string), 0, 64); e != nil {
40+
mode = defaultMode
41+
} else {
42+
mode = m
43+
}
4044
}
4145
}
4246
return mode

rules/sql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s *SqlStrConcat) checkObject(n *ast.Ident) bool {
4242
func (s *SqlStrConcat) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
4343
if node, ok := n.(*ast.BinaryExpr); ok {
4444
if start, ok := node.X.(*ast.BasicLit); ok {
45-
if str, _ := gas.GetString(start); s.pattern.MatchString(str) {
45+
if str, e := gas.GetString(start); s.pattern.MatchString(str) && e == nil {
4646
if _, ok := node.Y.(*ast.BasicLit); ok {
4747
return nil, nil // string cat OK
4848
}
@@ -77,7 +77,7 @@ type SqlStrFormat struct {
7777
// Looks for "fmt.Sprintf("SELECT * FROM foo where '%s', userInput)"
7878
func (s *SqlStrFormat) Match(n ast.Node, c *gas.Context) (gi *gas.Issue, err error) {
7979
if node := gas.MatchCall(n, s.call); node != nil {
80-
if arg, _ := gas.GetString(node.Args[0]); s.pattern.MatchString(arg) {
80+
if arg, e := gas.GetString(node.Args[0]); s.pattern.MatchString(arg) && e == nil {
8181
return gas.NewIssue(c, n, s.What, s.Severity, s.Confidence), nil
8282
}
8383
}

rules/tempfiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type BadTempFile struct {
2929

3030
func (t *BadTempFile) Match(n ast.Node, c *gas.Context) (gi *gas.Issue, err error) {
3131
if node := gas.MatchCall(n, t.call); node != nil {
32-
if arg, _ := gas.GetString(node.Args[0]); t.args.MatchString(arg) {
32+
if arg, e := gas.GetString(node.Args[0]); t.args.MatchString(arg) && e == nil {
3333
return gas.NewIssue(c, n, t.What, t.Severity, t.Confidence), nil
3434
}
3535
}

tools.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ func (u *utilities) run(args ...string) {
7474
func shouldSkip(path string) bool {
7575
st, e := os.Stat(path)
7676
if e != nil {
77+
// #nosec
7778
fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", path, e)
7879
return true
7980
}
8081
if st.IsDir() {
82+
// #nosec
8183
fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", path)
8284
return true
8385
}
@@ -95,11 +97,12 @@ func dumpAst(files ...string) {
9597
fset := token.NewFileSet() // positions are relative to fset
9698
f, err := parser.ParseFile(fset, arg, nil, 0)
9799
if err != nil {
100+
// #nosec
98101
fmt.Fprintf(os.Stderr, "Unable to parse file %s\n", err)
99102
continue
100103
}
101104

102-
// Print the AST.
105+
// Print the AST. #nosec
103106
ast.Print(fset, f)
104107
}
105108
}
@@ -115,7 +118,12 @@ type context struct {
115118

116119
func createContext(filename string) *context {
117120
fileset := token.NewFileSet()
118-
root, _ := parser.ParseFile(fileset, filename, nil, parser.ParseComments)
121+
root, e := parser.ParseFile(fileset, filename, nil, parser.ParseComments)
122+
if e != nil {
123+
// #nosec
124+
fmt.Fprintf(os.Stderr, "Unable to parse file: %s. Reason: %s\n", filename, e)
125+
return nil
126+
}
119127
comments := ast.NewCommentMap(fileset, root, root.Comments)
120128
info := &types.Info{
121129
Types: make(map[ast.Expr]types.TypeAndValue),
@@ -126,7 +134,12 @@ func createContext(filename string) *context {
126134
Implicits: make(map[ast.Node]types.Object),
127135
}
128136
config := types.Config{Importer: importer.Default()}
129-
pkg, _ := config.Check("main.go", fileset, []*ast.File{root}, info)
137+
pkg, e := config.Check("main.go", fileset, []*ast.File{root}, info)
138+
if e != nil {
139+
// #nosec
140+
fmt.Fprintf(os.Stderr, "Type check failed for file: %s. Reason: %s\n", filename, e)
141+
return nil
142+
}
130143
return &context{fileset, comments, info, pkg, &config, root}
131144
}
132145

@@ -147,13 +160,25 @@ func printObject(obj types.Object) {
147160
fmt.Printf(" Id = %v\n", obj.Id())
148161
}
149162

163+
func checkContext(ctx *context, file string) bool {
164+
// #nosec
165+
if ctx == nil {
166+
fmt.Fprintln(os.Stderr, "Failed to create context for file: ", file)
167+
return false
168+
}
169+
return true
170+
}
171+
150172
func dumpCallObj(files ...string) {
151173

152174
for _, file := range files {
153175
if shouldSkip(file) {
154176
continue
155177
}
156178
context := createContext(file)
179+
if !checkContext(context, file) {
180+
return
181+
}
157182
ast.Inspect(context.root, func(n ast.Node) bool {
158183
var obj types.Object
159184
switch node := n.(type) {
@@ -178,6 +203,9 @@ func dumpUses(files ...string) {
178203
continue
179204
}
180205
context := createContext(file)
206+
if !checkContext(context, file) {
207+
return
208+
}
181209
for ident, obj := range context.info.Uses {
182210
fmt.Printf("IDENT: %v, OBJECT: %v\n", ident, obj)
183211
}
@@ -190,6 +218,9 @@ func dumpTypes(files ...string) {
190218
continue
191219
}
192220
context := createContext(file)
221+
if !checkContext(context, file) {
222+
return
223+
}
193224
for expr, tv := range context.info.Types {
194225
fmt.Printf("EXPR: %v, TYPE: %v\n", expr, tv)
195226
}
@@ -202,6 +233,9 @@ func dumpDefs(files ...string) {
202233
continue
203234
}
204235
context := createContext(file)
236+
if !checkContext(context, file) {
237+
return
238+
}
205239
for ident, obj := range context.info.Defs {
206240
fmt.Printf("IDENT: %v, OBJ: %v\n", ident, obj)
207241
}
@@ -214,6 +248,9 @@ func dumpComments(files ...string) {
214248
continue
215249
}
216250
context := createContext(file)
251+
if !checkContext(context, file) {
252+
return
253+
}
217254
for _, group := range context.comments.Comments() {
218255
fmt.Println(group.Text())
219256
}
@@ -226,6 +263,9 @@ func dumpImports(files ...string) {
226263
continue
227264
}
228265
context := createContext(file)
266+
if !checkContext(context, file) {
267+
return
268+
}
229269
for _, pkg := range context.pkg.Imports() {
230270
fmt.Println(pkg.Path(), pkg.Name())
231271
for _, name := range pkg.Scope().Names() {

0 commit comments

Comments
 (0)