mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 03:55:54 +00:00
fix: parsing of the Go version (#844)
* fix: parsing of the Go version * fix: convert pseudo directive to comment
This commit is contained in:
parent
0c8e63ed86
commit
4e68fb5b15
2 changed files with 30 additions and 14 deletions
26
helpers.go
26
helpers.go
|
@ -34,8 +34,8 @@ import (
|
||||||
// initialization only imports.
|
// initialization only imports.
|
||||||
//
|
//
|
||||||
// Usage:
|
// Usage:
|
||||||
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
|
|
||||||
//
|
//
|
||||||
|
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
|
||||||
func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool) {
|
func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool) {
|
||||||
importedName, found := GetImportedName(pkg, c)
|
importedName, found := GetImportedName(pkg, c)
|
||||||
if !found {
|
if !found {
|
||||||
|
@ -474,9 +474,25 @@ func RootPath(root string) (string, error) {
|
||||||
|
|
||||||
// GoVersion returns parsed version of Go from runtime
|
// GoVersion returns parsed version of Go from runtime
|
||||||
func GoVersion() (int, int, int) {
|
func GoVersion() (int, int, int) {
|
||||||
versionParts := strings.Split(runtime.Version(), ".")
|
return parseGoVersion(runtime.Version())
|
||||||
major, _ := strconv.Atoi(versionParts[0][2:])
|
}
|
||||||
minor, _ := strconv.Atoi(versionParts[1])
|
|
||||||
build, _ := strconv.Atoi(versionParts[2])
|
// parseGoVersion parses Go version.
|
||||||
|
// example:
|
||||||
|
// - go1.19rc2
|
||||||
|
// - go1.19beta2
|
||||||
|
// - go1.19.4
|
||||||
|
// - go1.19
|
||||||
|
func parseGoVersion(version string) (int, int, int) {
|
||||||
|
exp := regexp.MustCompile(`go(\d+).(\d+)(?:.(\d+))?.*`)
|
||||||
|
parts := exp.FindStringSubmatch(version)
|
||||||
|
if len(parts) <= 1 {
|
||||||
|
return 0, 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
major, _ := strconv.Atoi(parts[1])
|
||||||
|
minor, _ := strconv.Atoi(parts[2])
|
||||||
|
build, _ := strconv.Atoi(parts[3])
|
||||||
|
|
||||||
return major, minor, build
|
return major, minor, build
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue