Debian Codegraph @

test/fixedbugs/issue28797.go

raw
1 // run
2
3 // Copyright 2018 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 (
10 "fmt"
11 )
12
13 // test expects f to panic, but not to run out of memory,
14 // which is a non-panic fatal error. OOM results from failure
15 // to properly check negative limit.
16 func test(f func()) {
17 defer func() {
18 r := recover()
19 if r == nil {
20 panic("panic wasn't recoverable")
21 }
22 }()
23 f()
24 }
25
26 //go:noinline
27 func id(x int) int {
28 return x
29 }
30
31 func main() {
32 test(foo)
33 test(bar)
34 }
35
36 func foo() {
37 b := make([]byte, 0)
38 b = append(b, 1)
39 id(len(b))
40 id(len(b) - 2)
41 s := string(b[1 : len(b)-2])
42 fmt.Println(s)
43 }
44
45 func bar() {
46 b := make([]byte, 1)
47 b = append(b, 1)
48 i := id(-1)
49 if i < len(b) { // establish value is not too large.
50 s := string(b[1:i]) // should check for negative also.
51 fmt.Println(s)
52 }
53 }