Skip to content

fix(G602): avoid false positives for range-over-array indexing#1531

Merged
ccojocar merged 1 commit intosecurego:masterfrom
ccojocar:fix_g602
Feb 19, 2026
Merged

fix(G602): avoid false positives for range-over-array indexing#1531
ccojocar merged 1 commit intosecurego:masterfrom
ccojocar:fix_g602

Conversation

@ccojocar
Copy link
Copy Markdown
Member

@ccojocar ccojocar commented Feb 19, 2026

This change fixes a false positive in G602 when iterating over fixed-size arrays with range and indexing using the loop variable.

  • Corrects loop bound normalization in SSA-based index analysis so offsets are not applied twice.
  • Preserves true-positive detection for real out-of-bounds patterns (for example index + 1 at upper edge).
  • Adds regression samples covering both:valid range-over-array indexing (no issue expected)invalid shifted indexing inside the same loop (issue expected)

fixes #1525

This change fixes a false positive in G602 when iterating over
fixed-size arrays with range and indexing using the loop variable.

Corrects loop bound normalization in SSA-based index analysis so offsets
are not applied twice.
Preserves true-positive detection for real out-of-bounds patterns (for
example index + 1 at upper edge).
Adds regression samples covering both:
valid range-over-array indexing (no issue expected)
invalid shifted indexing inside the same loop (issue expected)

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 72.41379% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.09%. Comparing base (caf93d0) to head (00e790d).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
analyzers/slice_bounds.go 72.41% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1531   +/-   ##
=======================================
  Coverage   80.08%   80.09%           
=======================================
  Files         101      101           
  Lines        9175     9198   +23     
=======================================
+ Hits         7348     7367   +19     
- Misses       1382     1384    +2     
- Partials      445      447    +2     

☔ 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 1735e5a into securego:master Feb 19, 2026
8 of 9 checks passed
@ccojocar ccojocar deleted the fix_g602 branch February 19, 2026 16:41
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.

False Positive "G602: slice index out of range" over "range" of array with given size

1 participant