2013-02-03 12:28:44 -07:00
|
|
|
// run
|
|
|
|
|
2016-04-10 15:32:26 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
2013-02-03 12:28:44 -07:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type T []int
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
n := -1
|
2016-08-25 06:17:52 -06:00
|
|
|
shouldPanic("len out of range", func() { _ = make(T, n) })
|
|
|
|
shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
|
|
|
|
shouldPanic("len out of range", func() { _ = make(T, int64(n)) })
|
|
|
|
shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) })
|
2013-02-03 12:28:44 -07:00
|
|
|
var t *byte
|
|
|
|
if unsafe.Sizeof(t) == 8 {
|
2016-08-25 06:17:52 -06:00
|
|
|
n = 1 << 20
|
2013-02-03 12:28:44 -07:00
|
|
|
n <<= 20
|
2016-08-25 06:17:52 -06:00
|
|
|
shouldPanic("len out of range", func() { _ = make(T, n) })
|
|
|
|
shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
|
2013-02-03 12:28:44 -07:00
|
|
|
n <<= 20
|
2016-08-25 06:17:52 -06:00
|
|
|
shouldPanic("len out of range", func() { _ = make(T, n) })
|
|
|
|
shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
|
2013-02-03 12:28:44 -07:00
|
|
|
} else {
|
|
|
|
n = 1<<31 - 1
|
2016-08-25 06:17:52 -06:00
|
|
|
shouldPanic("len out of range", func() { _ = make(T, n) })
|
|
|
|
shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
|
|
|
|
shouldPanic("len out of range", func() { _ = make(T, int64(n)) })
|
|
|
|
shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) })
|
2013-02-03 12:28:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldPanic(str string, f func()) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
panic("did not panic")
|
|
|
|
}
|
|
|
|
s := err.(error).Error()
|
|
|
|
if !strings.Contains(s, str) {
|
|
|
|
panic("got panic " + s + ", want " + str)
|
|
|
|
}
|
|
|
|
}()
|
2016-08-25 06:17:52 -06:00
|
|
|
|
2013-02-03 12:28:44 -07:00
|
|
|
f()
|
|
|
|
}
|