Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 1 | // run |
| 2 | |
Dan Willemsen | 0c15709 | 2016-07-08 13:57:52 -0700 | [diff] [blame] | 3 | // Copyright 2013 The Go Authors. All rights reserved. |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 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 | "strings" |
| 11 | "unsafe" |
| 12 | ) |
| 13 | |
| 14 | type T []int |
| 15 | |
| 16 | func main() { |
| 17 | n := -1 |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 18 | shouldPanic("len out of range", func() { _ = make(T, n) }) |
| 19 | shouldPanic("cap out of range", func() { _ = make(T, 0, n) }) |
| 20 | shouldPanic("len out of range", func() { _ = make(T, int64(n)) }) |
| 21 | shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) }) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 22 | var t *byte |
| 23 | if unsafe.Sizeof(t) == 8 { |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame] | 24 | // Test mem > maxAlloc |
| 25 | var n2 int64 = 1 << 59 |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 26 | shouldPanic("len out of range", func() { _ = make(T, int(n2)) }) |
| 27 | shouldPanic("cap out of range", func() { _ = make(T, 0, int(n2)) }) |
Colin Cross | 1371fe4 | 2019-03-19 21:08:48 -0700 | [diff] [blame] | 28 | // Test elem.size*cap overflow |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 29 | n2 = 1<<63 - 1 |
| 30 | shouldPanic("len out of range", func() { _ = make(T, int(n2)) }) |
| 31 | shouldPanic("cap out of range", func() { _ = make(T, 0, int(n2)) }) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 32 | } else { |
| 33 | n = 1<<31 - 1 |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 34 | shouldPanic("len out of range", func() { _ = make(T, n) }) |
| 35 | shouldPanic("cap out of range", func() { _ = make(T, 0, n) }) |
| 36 | shouldPanic("len out of range", func() { _ = make(T, int64(n)) }) |
| 37 | shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) }) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 38 | } |
Dan Willemsen | f3f2eb6 | 2018-08-28 11:28:58 -0700 | [diff] [blame] | 39 | |
| 40 | // Test make in append panics since the gc compiler optimizes makes in appends. |
| 41 | shouldPanic("len out of range", func() { _ = append(T{}, make(T, n)...) }) |
| 42 | shouldPanic("cap out of range", func() { _ = append(T{}, make(T, 0, n)...) }) |
| 43 | shouldPanic("len out of range", func() { _ = append(T{}, make(T, int64(n))...) }) |
| 44 | shouldPanic("cap out of range", func() { _ = append(T{}, make(T, 0, int64(n))...) }) |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func shouldPanic(str string, f func()) { |
| 48 | defer func() { |
| 49 | err := recover() |
| 50 | if err == nil { |
| 51 | panic("did not panic") |
| 52 | } |
| 53 | s := err.(error).Error() |
| 54 | if !strings.Contains(s, str) { |
| 55 | panic("got panic " + s + ", want " + str) |
| 56 | } |
| 57 | }() |
Dan Willemsen | bbdf664 | 2017-01-13 22:57:23 -0800 | [diff] [blame] | 58 | |
Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [diff] [blame] | 59 | f() |
| 60 | } |