Skip to content

False Positive for G602 with bounds check using switch #1250

Open
@theory

Description

Summary

I'm getting false positives for G602 when using a switch statement for bounds checking.

Steps to reproduce the behavior

Run gosec against this test case:

func main() {
	args := []any{"1"}
	switch len(args) - 1 {
	case 1:
		_ = args[1]
	}
}

Output:

[/Users/david/Downloads/try-gosec/main.go:7] - G602 (CWE-118): slice index out of range (Confidence: HIGH, Severity: LOW)
    6: 	case 1:
  > 7: 		_ = args[1]
    8: 	}

Autofix: 

Summary:
  Gosec  : dev
  Files  : 1
  Lines  : 9
  Nosec  : 0
  Issues : 1

gosec version

Just installed 1fb6a46 from GitHub.

Go version (output of 'go version')

go version go1.23.2 darwin/arm64

Operating system / Environment

macOS Sequoia

Expected behavior

No issues found.

Actual behavior

False positive for G602.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions