Skip to content
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

cmd/compile: missing BCE when index is modulo len #54755

Closed
tamird opened this issue Aug 29, 2022 · 3 comments
Closed

cmd/compile: missing BCE when index is modulo len #54755

tamird opened this issue Aug 29, 2022 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@tamird
Copy link
Contributor

tamird commented Aug 29, 2022

What version of Go are you using (go version)?

$ go version
go1.19

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
Linux amd64

What did you do?

package main

type foo struct {
	number int
}

var foos []foo

func init() {
	foos = make([]foo, 3)
}

func getFoo(n int) *foo {
	foos[len(foos)-1] = foo{} // IsInBounds
	if len(foos) != 0 {
		foos[len(foos)-1] = foo{} // No IsInBounds
		return &foos[n%len(foos)] // IsInBounds
	}
	return nil
}

func main() {

}

compiler output visible on godbolt

What did you expect to see?

No bounds checks.

What did you see instead?

Bounds check.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 29, 2022
@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 29, 2022
@heschi heschi added this to the Backlog milestone Aug 29, 2022
@heschi
Copy link
Contributor

heschi commented Aug 29, 2022

cc @golang/runtime

@randall77
Copy link
Contributor

randall77 commented Aug 29, 2022

i%len(a) is not necessarily in bounds of a if i is negative.

@tamird
Copy link
Contributor Author

tamird commented Aug 30, 2022

Good point; casting to uint removes the bounds check.

@tamird tamird closed this as completed Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants