gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/resolve.go (about) 1 // (c) Copyright 2016 Hewlett Packard Enterprise Development LP 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package gosec 16 17 import "go/ast" 18 19 func resolveIdent(n *ast.Ident, c *Context) bool { 20 21 if n.Obj == nil || n.Obj.Kind != ast.Var { 22 return true 23 } 24 if node, ok := n.Obj.Decl.(ast.Node); ok { 25 return TryResolve(node, c) 26 } 27 return false 28 } 29 30 func resolveAssign(n *ast.AssignStmt, c *Context) bool { 31 for _, arg := range n.Rhs { 32 if !TryResolve(arg, c) { 33 return false 34 } 35 } 36 return true 37 } 38 39 func resolveCompLit(n *ast.CompositeLit, c *Context) bool { 40 for _, arg := range n.Elts { 41 if !TryResolve(arg, c) { 42 return false 43 } 44 } 45 return true 46 } 47 48 func resolveBinExpr(n *ast.BinaryExpr, c *Context) bool { 49 return (TryResolve(n.X, c) && TryResolve(n.Y, c)) 50 } 51 52 func resolveCallExpr(n *ast.CallExpr, c *Context) bool { 53 // TODO(tkelsey): next step, full function resolution 54 return false 55 } 56 57 // TryResolve will attempt, given a subtree starting at some ATS node, to resolve 58 // all values contained within to a known constant. It is used to check for any 59 // unkown values in compound expressions. 60 func TryResolve(n ast.Node, c *Context) bool { 61 switch node := n.(type) { 62 case *ast.BasicLit: 63 return true 64 65 case *ast.CompositeLit: 66 return resolveCompLit(node, c) 67 68 case *ast.Ident: 69 return resolveIdent(node, c) 70 71 case *ast.AssignStmt: 72 return resolveAssign(node, c) 73 74 case *ast.CallExpr: 75 return resolveCallExpr(node, c) 76 77 case *ast.BinaryExpr: 78 return resolveBinExpr(node, c) 79 } 80 81 return false 82 }