Source file test/fixedbugs/bug273.go

     1  // run
     2  
     3  // Copyright 2010 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  // https://golang.org/issue/589
     8  
     9  package main
    10  
    11  import "unsafe"
    12  
    13  var bug = false
    14  
    15  var minus1 = -1
    16  var five = 5
    17  var big int64 = 10 | 1<<46
    18  
    19  type block [1 << 19]byte
    20  
    21  var g1 []block
    22  
    23  func shouldfail(f func(), desc string) {
    24  	defer func() { recover() }()
    25  	f()
    26  	if !bug {
    27  		println("BUG")
    28  		bug = true
    29  	}
    30  	println("didn't crash: ", desc)
    31  }
    32  
    33  func badlen() {
    34  	g1 = make([]block, minus1)
    35  }
    36  
    37  func biglen() {
    38  	g1 = make([]block, big)
    39  }
    40  
    41  func badcap() {
    42  	g1 = make([]block, 10, minus1)
    43  }
    44  
    45  func badcap1() {
    46  	g1 = make([]block, 10, five)
    47  }
    48  
    49  func bigcap() {
    50  	g1 = make([]block, 10, big)
    51  }
    52  
    53  type cblock [1<<16 - 1]byte
    54  
    55  var g4 chan cblock
    56  
    57  func badchancap() {
    58  	g4 = make(chan cblock, minus1)
    59  }
    60  
    61  func bigchancap() {
    62  	g4 = make(chan cblock, big)
    63  }
    64  
    65  func overflowchan() {
    66  	const ptrSize = unsafe.Sizeof(uintptr(0))
    67  	g4 = make(chan cblock, 1<<(30*(ptrSize/4)))
    68  }
    69  
    70  func main() {
    71  	shouldfail(badlen, "badlen")
    72  	shouldfail(biglen, "biglen")
    73  	shouldfail(badcap, "badcap")
    74  	shouldfail(badcap1, "badcap1")
    75  	shouldfail(bigcap, "bigcap")
    76  	shouldfail(badchancap, "badchancap")
    77  	shouldfail(bigchancap, "bigchancap")
    78  	shouldfail(overflowchan, "overflowchan")
    79  }
    80  

View as plain text