-
-
Notifications
You must be signed in to change notification settings - Fork 621
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
false positive of g602 with maps #1005
Comments
This looks like a false positive since the initial capacity of the map doesn't restrict its size. @morgenm Please could you have a look at this issue, and exclude this use case from the G602 check? Thanks |
+1 |
It's the same with the slice. if len(items) > 0 {
return items[0], nil
} |
@micronull could you please specify how you create the |
I don't know what it has to do with how it's made. At least the example shown below is normal and common code, and I think it's safe to say it's a false positive. (I used google translate)
|
items := make([]T, 0)
if err := jsoniter.NewDecoder(res.Body).Decode(&items); err != nil {
return nil, fmt.Errorf(`%w: %w`, ErrDecodeResponse, err)
}
if len(items) == 0 {
return nil, ErrEmptyResponse
}
return &items[0], nil |
same here: func firstNAndSomeMore(names []string) string {
if len(names) > 3 {
return fmt.Sprintf("%s, and %d more", strings.Join(names[:3], ", "), len(names)-3)
}
return strings.Join(names, ", ")
} is reported as an error through $ golangci-lint --version
golangci-lint has version 1.54.2 built with go1.21.0 from 411e0bb on 2023-08-21T11:04:00Z |
@ccojocar I'm having the same issue with slices. In my case, I have code that looks like this: candidates := make([]string, 0)
for _, template := range templates {
if strings.HasPrefix(template.Name, namePrefix) {
candidates = append(candidates, template.Name)
}
}
if len(candidates) == 0 {
util.Fatalf(NoMatchingLaunchTemplates, "No matching launch templates found.\n")
} else if len(candidates) > 1 {
util.Fatalf(MultipleLaunchTemplates, "Multiple launch templates found.\n")
}
name := candidates[0] Even if I wrap the last line up like so, I still get the error: var name string
if len(candidates) > 0 {
name = candidates[0]
} |
I have a similar issue using an index into a slice that should be ok.
Command used is Message
|
I know that I probably should make a different Issue, but also having issues with slices on latest golangci-lint 1.54.2 validationErrors := make([]error, 0)
// some code
if len(validationErrors) == 1 {
return validationErrors[0] // this one false positive
} |
Summary
When setting keys in maps which obviously fon't exist before we get an error message
Steps to reproduce the behavior
gosec version
v2.17.0
Go version (output of 'go version')
GitHub Actions / golangci
golangci-lint-1.54.2, go 1.19
Operating system / Environment
ubuntu/ gh actions
Expected behavior
no error
Actual behavior
gives error
The text was updated successfully, but these errors were encountered: