gosec: G115: integer overflow conversion uint8 -> int64
Summary
uint8 -> int64 has no overflow
Steps to reproduce the behavior
package main
import (
"fmt"
)
func main() {
str := "A\xFF"
i := int64(str[0])
fmt.Printf("%d\n", i)
}
gosec version
81cda2f91fbe
Go version (output of ‘go version’)
go version go1.22.6 darwin/arm64
Operating system / Environment
n/a
Expected behavior
no complaint
Actual behavior
complains G115: integer overflow conversion uint8 -> int64:
main.go:9:12: G115: integer overflow conversion uint8 -> int64 (gosec)
i := int64(str[0])
^
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 16
- Comments: 21 (11 by maintainers)
Commits related to this issue
- chore: Add //nolint:gosec ParseInt(x,x,32) makes sure it will fit into int32. See also https://github.com/securego/gosec/issues/1185 Signed-off-by: Víctor Cuadrado Juan <vcuadradojuan@suse.de> — committed to kubewarden/kubewarden-controller by viccuad 2 years ago
- chore: Add //nolint:gosec ParseInt(x,x,32) makes sure it will fit into int32. See also https://github.com/securego/gosec/issues/1185 Signed-off-by: Víctor Cuadrado Juan <vcuadradojuan@suse.de> — committed to kubewarden/kubewarden-controller by viccuad 2 years ago
- ci(golangci-lint): disable gosec G115 Changes were made to this check in v1.60.2, which introduced a lot of noise and false positives. This commit disables this for now. Ref: https://github.com/secu... — committed to snprajwal/go-criu by snprajwal 2 years ago
- chore: disable gosec(G115) A change to the rule gosec(G115) made a large amount of FP for gosec appear when updating to the latest golang-ci linter. https://github.com/securego/gosec/issues/1185 htt... — committed to anchore/syft by spiffcs 2 years ago
- chore(deps): update tools to latest versions (#3205) * chore(deps): update tools to latest versions Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * ... — committed to anchore/syft by anchore-actions-token-generator[bot] 2 years ago
- chore: disable gosec(G115) Ignoring this rule as this doesn't break anything but causes lint errors. Upstream ref: https://github.com/securego/gosec/issues/1185 — committed to grafana/terraform-provider-grafana by Duologic 2 years ago
- Bump github.com/grafana/synthetic-monitoring-agent from 0.25.2 to 0.28.0 (#1813) * Bump github.com/grafana/synthetic-monitoring-agent from 0.25.2 to 0.28.0 Bumps [github.com/grafana/synthetic-moni... — committed to grafana/terraform-provider-grafana by dependabot[bot] 2 years ago
- Bump github.com/grafana/synthetic-monitoring-agent from 0.25.2 to 0.28.0 (#1813) * Bump github.com/grafana/synthetic-monitoring-agent from 0.25.2 to 0.28.0 Bumps [github.com/grafana/synthetic-moni... — committed to grafana/terraform-provider-grafana by dependabot[bot] 2 years ago
Agree, lots of alerts from yesterday in our CI/CD pipeline
I was also surprised to see #1149 being merged so fast.
While the idea is good, why not after all. But it should have been reviewed by testing the behavior of the rules on large codebase.
Or make this rule optional at first
I can reproduce the “uint8 -> int” overflow false positive on my side. See the screenshot.
FYI I’m using golangci-lint v1.60.2 (I didn’t try gosec itself)
Broken in this change I guess https://github.com/securego/gosec/issues/1130
This seems like a seriously under-cooked change which is currently mandating a lot of //nolint flags for us.
Example of another goof ->
foo := []int{1,2,3}; bar := uint32(len(foo))cannot possibly cause data loss yet now fails.I’ve opened a discussion on golangci’s repo as this is probably more of a concern for folks doing CI than people wanting to do one-off security sweeps, where detecting possible overflows is of higher value 😃 https://github.com/golangci/golangci-lint/discussions/4939
@xsteadfastx your options are:
I’ve gone with a mix of 2 and 3 for the time being.
I suspect there’s other folks like me who got caught out here when pulling a latest from golang-ci. Given other bossy rules from gosec are globally-disabled there, I might raise an issue there to see if G115 should get added.
@FairlySadPanda the
lenfunction returns anintwhich is variable based on which architecture are you running. So in a 64 bit arch which is the most common these days, the value is actually int64. In the end, you are converting fromint64touin32this is a clear overflow.Yes same here, could you add documentation about how to make G115 pass? Otherwise we are going to ignore all alerts 😦 We’ve tried to add bound checks and nothing works
For info, G115 was disabled by default in golangci-lint
@xsteadfastx the
strconv.Atoireturns an int type which is typically int64 in a 64bit CPU architecture. This is safe to convert to auint64. The rule doesn’t generate any warning in this case. This use case is coverted by tests.If you try to scan this code sample, you can see that gosec doesn’t return any warning:
FIY bounds checks which are not implicit in the int type are not yet handled, this should be addressed by #1187. Please add any code sample which handles the bound checks explicitly in that issue. Thanks