1
0
mirror of https://github.com/securego/gosec.git synced 2025-11-23 22:15:04 +02:00

Small update to G201 and added ConcatString Function (#228)

This commit is contained in:
cschoenduve-splunk
2018-08-19 10:57:36 -07:00
committed by Cosmin Cojocar
parent 1c438e36af
commit a7cff91312
2 changed files with 35 additions and 0 deletions

View File

@@ -256,3 +256,28 @@ func GetPkgAbsPath(pkgPath string) (string, error) {
}
return absPath, nil
}
// ConcatString recusively concatenates strings from a binary expression
func ConcatString(n *ast.BinaryExpr) (string, bool) {
var s string
// sub expressions are found in X object, Y object is always last BasicLit
if rightOperand, ok := n.Y.(*ast.BasicLit); ok {
if str, err := GetString(rightOperand); err == nil {
s = str + s
}
} else {
return "", false
}
if leftOperand, ok := n.X.(*ast.BinaryExpr); ok {
if recursion, ok := ConcatString(leftOperand); ok {
s = recursion + s
}
} else if leftOperand, ok := n.X.(*ast.BasicLit); ok {
if str, err := GetString(leftOperand); err == nil {
s = str + s
}
} else {
return "", false
}
return s, true
}