Skip to content

Commit 4e68fb5

Browse files
authored
fix: parsing of the Go version (#844)
* fix: parsing of the Go version * fix: convert pseudo directive to comment
1 parent 0c8e63e commit 4e68fb5

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

cmd/gosec/main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (a *arrayFlags) Set(value string) error {
7171
}
7272

7373
var (
74-
//#nosec flag
74+
// #nosec flag
7575
flagIgnoreNoSec = flag.Bool("nosec", false, "Ignores #nosec comments when set")
7676

7777
// show ignored
@@ -80,7 +80,7 @@ var (
8080
// format output
8181
flagFormat = flag.String("fmt", "text", "Set output format. Valid options are: json, yaml, csv, junit-xml, html, sonarqube, golint, sarif or text")
8282

83-
//#nosec alternative tag
83+
// #nosec alternative tag
8484
flagAlternativeNoSec = flag.String("nosec-tag", "", "Set an alternative string for #nosec. Some examples: #dontanalyze, #falsepositive")
8585

8686
// output file
@@ -148,7 +148,7 @@ var (
148148
logger *log.Logger
149149
)
150150

151-
//#nosec
151+
// #nosec
152152
func usage() {
153153
usageText := fmt.Sprintf(usageText, Version, GitTag, BuildDate)
154154
fmt.Fprintln(os.Stderr, usageText)
@@ -173,12 +173,12 @@ func usage() {
173173
func loadConfig(configFile string) (gosec.Config, error) {
174174
config := gosec.NewConfig()
175175
if configFile != "" {
176-
//#nosec
176+
// #nosec
177177
file, err := os.Open(configFile)
178178
if err != nil {
179179
return nil, err
180180
}
181-
defer file.Close() //#nosec G307
181+
defer file.Close() // #nosec G307
182182
if _, err := config.ReadFrom(file); err != nil {
183183
return nil, err
184184
}
@@ -253,11 +253,11 @@ func printReport(format string, color bool, rootPaths []string, reportInfo *gose
253253
}
254254

255255
func saveReport(filename, format string, rootPaths []string, reportInfo *gosec.ReportInfo) error {
256-
outfile, err := os.Create(filename) //#nosec G304
256+
outfile, err := os.Create(filename) // #nosec G304
257257
if err != nil {
258258
return err
259259
}
260-
defer outfile.Close() //#nosec G307
260+
defer outfile.Close() // #nosec G307
261261
err = report.CreateReport(outfile, format, false, rootPaths, reportInfo)
262262
if err != nil {
263263
return err
@@ -337,7 +337,7 @@ func main() {
337337

338338
// Ensure at least one file was specified or that the recursive -r flag was set.
339339
if flag.NArg() == 0 && !*flagRecursive {
340-
fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' or -r expected\n") //#nosec
340+
fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' or -r expected\n") // #nosec
341341
flag.Usage()
342342
os.Exit(1)
343343
}
@@ -460,7 +460,7 @@ func main() {
460460
}
461461

462462
// Finalize logging
463-
logWriter.Close() //#nosec
463+
logWriter.Close() // #nosec
464464

465465
exit(issues, errors, *flagNoFail)
466466
}

helpers.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
// initialization only imports.
3535
//
3636
// Usage:
37-
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
3837
//
38+
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
3939
func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool) {
4040
importedName, found := GetImportedName(pkg, c)
4141
if !found {
@@ -474,9 +474,25 @@ func RootPath(root string) (string, error) {
474474

475475
// GoVersion returns parsed version of Go from runtime
476476
func GoVersion() (int, int, int) {
477-
versionParts := strings.Split(runtime.Version(), ".")
478-
major, _ := strconv.Atoi(versionParts[0][2:])
479-
minor, _ := strconv.Atoi(versionParts[1])
480-
build, _ := strconv.Atoi(versionParts[2])
477+
return parseGoVersion(runtime.Version())
478+
}
479+
480+
// parseGoVersion parses Go version.
481+
// example:
482+
// - go1.19rc2
483+
// - go1.19beta2
484+
// - go1.19.4
485+
// - go1.19
486+
func parseGoVersion(version string) (int, int, int) {
487+
exp := regexp.MustCompile(`go(\d+).(\d+)(?:.(\d+))?.*`)
488+
parts := exp.FindStringSubmatch(version)
489+
if len(parts) <= 1 {
490+
return 0, 0, 0
491+
}
492+
493+
major, _ := strconv.Atoi(parts[1])
494+
minor, _ := strconv.Atoi(parts[2])
495+
build, _ := strconv.Atoi(parts[3])
496+
481497
return major, minor, build
482498
}

0 commit comments

Comments
 (0)