-
-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: G602 Slice Bound Checking #973
Conversation
rules/slice_bounds.go
Outdated
return nil, nil | ||
} | ||
|
||
// NewSliceBoundsCheck attempts to find any slices being accessed out of bounds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment on exported function NewSliceBoundCheck should be of the form "NewSliceBoundCheck ..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please fix the function name in the comment?
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## master #973 +/- ##
==========================================
+ Coverage 71.84% 72.08% +0.24%
==========================================
Files 50 51 +1
Lines 3317 3586 +269
==========================================
+ Hits 2383 2585 +202
- Misses 868 911 +43
- Partials 66 90 +24
☔ View full report in Codecov by Sentry. |
Note: This solution is complex since it stores the constant capacities of all the slices made with Let me know your thoughts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this great contribution. I left a few comments. It would be great if you could fix them before we merge this rule. Thanks again!
rules/slice_bounds.go
Outdated
continue | ||
} | ||
|
||
paramName := params[it].Names[0].Name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check Name[0]
that is not nil before accessing the Name
filed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I added that check.
rules/slice_bounds.go
Outdated
s.sliceCaps[nil] = sliceMap | ||
} | ||
|
||
// Matches calls to make() and stores the capacity of the new slice in the map to compare against future slice usage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method name prefix in the comment
rules/slice_bounds.go
Outdated
return caps | ||
} | ||
|
||
// Matches slice assignments, calculates capacity of slice if possible to store it in map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function name prefix missing in the comment
rules/slice_bounds.go
Outdated
func (s *sliceOutOfBounds) matchSliceAssignment(node *ast.SliceExpr, sliceName string, ctx *gosec.Context) (*issue.Issue, error) { | ||
// First do the normal match that verifies the slice expr is not out of bounds | ||
if i, err := s.matchSliceExpr(node, ctx); err != nil { | ||
return i, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would wrap the err with some more context before returning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I wrapped it using fmt.Errorf
.
rules/slice_bounds.go
Outdated
return nil, nil | ||
} | ||
|
||
// NewSliceBoundsCheck attempts to find any slices being accessed out of bounds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please fix the function name in the comment?
I think the current solution which checks the constant bounds looks fine to me. It is non intrusive comparing with a bound check which is more enforced on user. My impression is, that one will really check the bounds at runtime if she wants to be defensive but most of people won't do it. This will cause a lot of warnings which most likely will get ignored. |
I believe I addressed all the reviews.
Yes, thinking on it some more I agree with you. |
It would be nice if this didn't fire when it was protected by something like |
Yes, this rule fires a lot of false positives on code protected with something like |
This PR addresses this issue: #954
This adds a new rule which checks slice bounds access (reslicing and indexing) to ensure that slices are not accessed out of bounds. This only works for slice bounds defined by a
make([]..., len)
ormake([]..., len, capacity)
where len/capacity is defined as a literal integer. For example it works for:But not for:
Since we don't evaluate expressions. It also will calculate and validate slice capacities for new slices made by reslicing when possible, such as:
The capacity for the new slice,
x
, will be validated in any subsequent usage. In addition, slices passed to functions are validated when possible by storing function-call-specific capacities. For example:will cause an error on creation of
newSlice
andnewSlice2
, since whenz
is passed to it, it is accessed out of bounds.