KEMBAR78
check nil slices, partially check bounds by kondratev · Pull Request #1396 · securego/gosec · GitHub
Skip to content

Conversation

kondratev
Copy link
Contributor

fix #1392 partially

Copy link
Member

@ccojocar ccojocar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing this fix. Please add some tests in the rule test file to cover this use case https://github.com/securego/gosec/blob/master/testutils/g602_samples.go.

case *ssa.Store: // check store to nil slice or out of bounds
case *ssa.IndexAddr:
if constantValue, ok := instr.X.(*ssa.Const); ok && constantValue.Type().String()[:2] == "[]" {
if constantValue.Value == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this check be placed before accessing the .Type()? What happens if is already nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that someone will continue to search for errors. For example, when var a [5]int this const value not nil, in this case the simplest error is determined when instead of append(a,...) index assignment is used.

}
case *ssa.Store: // check store to nil slice or out of bounds
case *ssa.IndexAddr:
if constantValue, ok := instr.X.(*ssa.Const); ok && constantValue.Type().String()[:2] == "[]" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the type length be less than 2 under any circumstances?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if type name string less then []*, then this is not slice in this case

@kondratev
Copy link
Contributor Author

@ccojocar review please

@ccojocar
Copy link
Member

ccojocar commented Oct 1, 2025

There are some lint warnings which need to be fixed before merging, otherwise LGTM.

@ccojocar
Copy link
Member

ccojocar commented Oct 1, 2025

It seems that there is a test failure.

@ccojocar
Copy link
Member

ccojocar commented Oct 2, 2025

There are still some issues when the gosec built from this pull request. PTAL. Thanks

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 84.53608% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.01%. Comparing base (1216c9b) to head (33cce17).
⚠️ Report is 117 commits behind head on master.

Files with missing lines Patch % Lines
analyzers/slice_bounds.go 84.53% 11 Missing and 4 partials ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1396      +/-   ##
==========================================
- Coverage   68.49%   63.01%   -5.49%     
==========================================
  Files          75       76       +1     
  Lines        4384     5443    +1059     
==========================================
+ Hits         3003     3430     +427     
- Misses       1233     1878     +645     
+ Partials      148      135      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ccojocar ccojocar merged commit 01029f0 into securego:master Oct 3, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

G602 doesn't detect unallocated slices

3 participants