Skip to content

fix(G602): validate subslice bounds under equality guards - #1749

Merged
ccojocar merged 1 commit into
securego:masterfrom
gulamovzavohir02-glitch:fix/g602-equality-slice-bounds
Sep 16, 2026
Merged

ccojocar merged 1 commit into
securego:masterfrom
gulamovzavohir02-glitch:fix/g602-equality-slice-bounds

Conversation

@gulamovzavohir02-glitch

Copy link
Copy Markdown
Contributor

Fixes the subslice equality-guard defect noted as out of scope in #1746; related to #1727.

Under if len(s) == 3, G602 reports safe subslices such as s[:3] and s[1:3]. Conversely, a three-index subslice such as s[:3:3] in the else branch can have its finding incorrectly cleared. The existing *ssa.Slice arm compares only the maximum index against the equality constant, ignoring the actual low/high bounds and treating both successors as if equality held.

The change checks low, high, and maximum bounds against the asserted length, including constant offsets in the guard, and only clears findings in the equality branch. Omitted low/high bounds use their Go defaults; unknown explicit bounds remain conservative. The implementation reuses the existing three-index bounds predicate and leaves the neighboring index-access logic unchanged.

Validation:

  • 15 regression samples covering two- and three-index slices, omitted bounds, reversed comparisons, constant offsets, unsafe bounds, the else branch, and dynamic bounds.
  • The new samples fail against the original analyzer; focused G602 tests pass with the fix.
  • CLI comparison across the same samples removes seven false positives and restores one missed finding; all seven expected findings remain.
  • go test ./...
  • go build ./cmd/gosec
  • golangci-lint run (v2.13.2): 0 issues.
  • Built gosec scanning this repository (-exclude-dir=testdata ./...): 0 findings.

This does not claim to resolve the nil-slice or append-tracking cases in #1727. Developed with AI assistance.

Signed-off-by: Jasur Gulyamov <gulamovzavohir02@gmail.com>
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.92%. Comparing base (accb424) to head (cd58316).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1749      +/-   ##
==========================================
+ Coverage   84.88%   84.92%   +0.04%     
==========================================
  Files         110      110              
  Lines        9055     9063       +8     
==========================================
+ Hits         7686     7697      +11     
+ Misses       1369     1366       -3     

☔ View full report in Codecov by Harness.
📢 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 201d413 into securego:master Sep 16, 2026
14 checks passed
cipherprofessor added a commit to cipherprofessor/gosec that referenced this pull request Sep 29, 2026
…ices

A nil-declared slice's IndexAddr was flagged unconditionally regardless of
any surrounding "if len(s) == N" guard, because it's caught by an early,
value-based check that never enters the trackSliceBounds/ifs correlation
pipeline securego#1746/securego#1749 use to trust an equality guard for make()/literal
slices. Same guard shape, inconsistent answer, purely because of how the
empty slice was constructed -- verified against master via an SSA dump
(nil case resolves to a bare *ssa.Const, the make() case to a *ssa.Slice
with a real Alloc, only the latter feeds the existing pipeline).

Defer nil-slice-constant accesses instead of flagging them immediately,
separately collect "len(nilConst) == N" guards (reusing the existing
extractSliceIfLenCondition helper), and resolve each access after its
function is fully walked: if the guard's safe (then) successor block
dominates the access and the index fits the asserted length, trust it,
same as the make() case already does.

Keying is by the specific *ssa.Const each len()/access involves, not by
type -- verified empirically that Go's SSA builder allocates a distinct
constant per nil-typed variable even when the type is identical, so this
can't confuse "var a []int" with "var b []int" sharing a type. A guard is
only trusted when it's the sole one recorded for its const; multiple
guards on the same const fall back to flagging unconditionally, same as
before this change.

Adds regression coverage for: a nil slice safely covered by a guard, one
where the index isn't covered, two distinct same-type nil slices each
independently guarded and safe, and two distinct nil slices where only
one is guarded (the unguarded one must still fire).

Fixes the remaining part of securego#1727 not already covered by securego#1746/securego#1749.
ccojocar pushed a commit that referenced this pull request Oct 2, 2026
…ices (#1752)

A nil-declared slice's IndexAddr was flagged unconditionally regardless of
any surrounding "if len(s) == N" guard, because it's caught by an early,
value-based check that never enters the trackSliceBounds/ifs correlation
pipeline #1746/#1749 use to trust an equality guard for make()/literal
slices. Same guard shape, inconsistent answer, purely because of how the
empty slice was constructed -- verified against master via an SSA dump
(nil case resolves to a bare *ssa.Const, the make() case to a *ssa.Slice
with a real Alloc, only the latter feeds the existing pipeline).

Defer nil-slice-constant accesses instead of flagging them immediately,
separately collect "len(nilConst) == N" guards (reusing the existing
extractSliceIfLenCondition helper), and resolve each access after its
function is fully walked: if the guard's safe (then) successor block
dominates the access and the index fits the asserted length, trust it,
same as the make() case already does.

Keying is by the specific *ssa.Const each len()/access involves, not by
type -- verified empirically that Go's SSA builder allocates a distinct
constant per nil-typed variable even when the type is identical, so this
can't confuse "var a []int" with "var b []int" sharing a type. A guard is
only trusted when it's the sole one recorded for its const; multiple
guards on the same const fall back to flagging unconditionally, same as
before this change.

Adds regression coverage for: a nil slice safely covered by a guard, one
where the index isn't covered, two distinct same-type nil slices each
independently guarded and safe, and two distinct nil slices where only
one is guarded (the unguarded one must still fire).

Fixes the remaining part of #1727 not already covered by #1746/#1749.

This branch was previously deployed

1 inactive deployment
security-review — cd583161 Deployed Sep 16, 2026 by gulamovzavohir02-glitch via barry-ai-security-review #2573
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.

2 participants