Skip to content

cmd/compile: incorrect slice-length bound inference can cause miscompilation #76709

@jake-ciolek

Description

@jake-ciolek

Go version

91267f0

Output of go env in your module/workspace:

GOARCH=amd64

What did you do?

The following code behaves differently on 91267f and 1.25.5:

package main

import "fmt"

//go:noinline
func bug1(a []int, i int) int {
	if i < 0 || i > 20-len(a) {
		return 0
	}
	diff := len(a) - i
	if diff < 10 {
		return 1
	}
	return 2
}

//go:noinline
func bug2(s []int, i int) int {
	if i < 0 {
		return 0
	}
	if i <= 10-len(s) {
		x := len(s) - i
		return x / 2
	}
	return 0
}

func main() {
	if got := bug1(make([]int, 5), 15); got != 1 {
		println(fmt.Sprintf("bug1: got %d, want 1", got))
	}
	if got := bug2(make([]int, 3), 7); got != -2 {
		println(fmt.Sprintf("bug2: got %d, want -2", got))
	}
}

What did you see happen?

On tip we get:

bug1: got 2, want 1
bug2: got 9223372036854775806, want -2

At 1.25.5, the program runs correctly.

What did you expect to see?

Same behavior as on the release version.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.release-blocker

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions