| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import "fmt" |
| 10 |
|
| 11 |
//go:noinline |
| 12 |
func bug1(a []int, i int) int { |
| 13 |
if i < 0 || i > 20-len(a) { |
| 14 |
return 0 |
| 15 |
} |
| 16 |
diff := len(a) - i |
| 17 |
if diff < 10 { |
| 18 |
return 1 |
| 19 |
} |
| 20 |
return 2 |
| 21 |
} |
| 22 |
|
| 23 |
//go:noinline |
| 24 |
func bug2(s []int, i int) int { |
| 25 |
if i < 0 { |
| 26 |
return 0 |
| 27 |
} |
| 28 |
if i <= 10-len(s) { |
| 29 |
x := len(s) - i |
| 30 |
return x / 2 |
| 31 |
} |
| 32 |
return 0 |
| 33 |
} |
| 34 |
|
| 35 |
func main() { |
| 36 |
if got := bug1(make([]int, 5), 15); got != 1 { |
| 37 |
panic(fmt.Sprintf("bug1: got %d, want 1", got)) |
| 38 |
} |
| 39 |
if got := bug2(make([]int, 3), 7); got != -2 { |
| 40 |
panic(fmt.Sprintf("bug2: got %d, want -2", got)) |
| 41 |
} |
| 42 |
} |