Repository navigation
Fix gosec G601: Implicit memory aliasing of items from a range statement - #6551
Conversation
|
@wallrj general remark:
for _, crt := range crts {
crt := crtor equivalent (but less clear) for i := range crts {
crt := crts[i]
for i := range crts {
crt := &crts[i]I think we should use 1 whenever possible and use 2 in case we are working with large objects and want to avoid a lot of memory being allocated (eg. in case of the list.Items loops). |
| var owned []*cmacme.Order | ||
| for _, o := range l.Items { | ||
| for i := range l.Items { | ||
| o := &l.Items[i] |
There was a problem hiding this comment.
nit: We might want to copy the values, so the underlying array can be gc'ed.
This is ok since it's just test code.
There was a problem hiding this comment.
I hadn't even considered it.
I don't know how Go garbage collection works.
If there's a block of memory somewhere containing this Order{} object and o is a pointer to it which lives beyond the scope of this loop, why should that prevent the garbage collector freeing up the memory used by the other Orders in that list?
There was a problem hiding this comment.
There was a problem hiding this comment.
Very interesting, thanks!
In that case I'll alter the commit and ping you when done.
commented
Dec 13, 2023
|
The golangci-lint config file currently ignores these issues in |
commented
Dec 13, 2023
|
@SgtCoDFish: Overrode contexts on behalf of SgtCoDFish: pull-cert-manager-master-license DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
commented
Dec 13, 2023
|
/retest |
1 similar comment
commented
Dec 13, 2023
|
/retest |
commented
Dec 13, 2023
|
/lgtm |
Signed-off-by: Richard Wall <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
commented
Dec 20, 2023
|
@inteon I've revisited each of the warnings and decided that most of them are false positives and annotated them as such. I've also updated the PR description with my latest understanding of all this. PTAL |
left a comment
•
There was a problem hiding this comment.
Thank you for making this change.
I agree that this is a false-positive in case the lifetime of the variable does not exceed the for loop body scope.
I think golangci-lint needs a smarter check, using some kind of borrow-checker (it should be more like rust 😄 ).
/approve
/lgtm
commented
Dec 20, 2023
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: inteon The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
commented
Dec 20, 2023
|
/unhold |
_test.gofilesG601 is described in https://github.com/securego/gosec#available-rules as follows:
This gosec check was proposed in securego/gosec#438 and implemented in securego/gosec#443.
It is intended to identify instances of the problem described in The Go Common Mistakes section: Using reference to loop iterator variable, but the check suffers from the problems described by RSC in golang/go#56010:
Most of these are false positives and I've annotated these using the gosec
# nosecannotation,because it allows me to specify exactly which issue I want to ignore.
The alternative is
// nolint:gosecwhich is less specific:I wasn't aware when I started this PR, that the check (G601), these changes and annotations will become obsolete upon the release of Go 1.22 which changes the scope of the loop var. Once cert-manager upgrades to Go 1.22 we can probably revert all these changes:
Testing
Example failures:
You can also run the checks locally as follows:
/kind cleanup