-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloser.go
114 lines (108 loc) · 2.79 KB
/
closer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018 Terence Tarvis. All rights reserved.
package main
import (
"go/ast"
)
func init() {
register("closeCheck",
"this tests if things with .Close() method have .Close() actually called on them",
closeCheck,
funcDecl)
}
func opensFile(f *File, x ast.Expr) bool {
/*
if(f.pkg.info.TypeOf(x) == nil) {
// should probably print something out here to notify the user
return false;
}
*/
/*
if(f.pkg.info.TypeOf(x).String() == "(*os.File, error)") {
return true
}
*/
if typeValue := f.pkg.info.TypeOf(x); typeValue != nil {
if typeValue.String() == "(*os.File, error)" {
return true;
}
}
return false;
}
// closesFile checks the remaining statements in a function body for a .Close() method
func closesFile(f *File, stmts []ast.Stmt) bool {
for _, stmt := range stmts {
switch expr := stmt.(type) {
case *ast.AssignStmt:
rhs := expr.Rhs;
for _, x := range rhs {
name, err := getFullFuncName(x);
if err != nil {
warnf("issue, %v", err);
}
if(name == "file/Close") {
return true
}
}
case *ast.ExprStmt:
name, err := getFullFuncName(expr.X);
if err != nil {
warnf("issue, %v", err);
}
if(name == "file/Close") {
return true
}
}
}
return false
}
// for the time being this just checks a function to see if an opened file is closed
// http.MaxBytesReader should also be checked for a close
func closeCheck(f *File, node ast.Node) {
var formatString string = "Audit for Close() method called on opened file, %s"
// loop through block
// look for file open
// look for file close
// if no file close, report
// consider checking if the opened file is returned
// consider checking if an open file is an input and no file is returned
// it turns out you can open a file and not use it. What then?
// ugh I really hate this
// is walking the statements a better option?
if fun, ok := node.(*ast.FuncDecl); ok {
// todo: check that this prevents crashes
if (fun.Body == nil || fun.Body.List == nil) { return; }
for i, stmts := range fun.Body.List {
switch stmt := stmts.(type) {
case *ast.AssignStmt:
rhs := stmt.Rhs;
for _, x := range rhs {
if(opensFile(f, x)) {
if(!closesFile(f, fun.Body.List[i:])) {
f.Reportf(stmt.Pos(), formatString,f.ASTString(x))
}
}
}
case *ast.ExprStmt:
if(opensFile(f, stmt.X)) {
if(!closesFile(f, fun.Body.List[i:])) {
f.Reportf(stmt.Pos(), formatString, f.ASTString(stmt.X))
}
}
case *ast.IfStmt:
if s, ok := stmt.Init.(*ast.AssignStmt); ok {
rhs := s.Rhs;
for _, x := range rhs {
if(opensFile(f, x )) {
if(!closesFile(f, fun.Body.List[i:])) {
f.Reportf(stmt.Pos(), formatString, f.ASTString(x))
}
}
}
}
default:
// do nothing for time being
}
}
}
return;
}