From 9ebad4b01c22d4c03a3552fd6b0e86c9de0ce6bd Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Sat, 28 Nov 2020 06:48:54 -0800 Subject: compiler, runtime: check len/cap for append(s, make(T, l)...) The overflow checks done in growslice always reported an error for the capacity argument, even if it was the length argument that overflowed. This change lets the code pass the current issue4085b.go test. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273806 --- libgo/go/runtime/slice.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'libgo/go/runtime') diff --git a/libgo/go/runtime/slice.go b/libgo/go/runtime/slice.go index 97b2659..ddd588e 100644 --- a/libgo/go/runtime/slice.go +++ b/libgo/go/runtime/slice.go @@ -15,6 +15,7 @@ import ( //go:linkname panicmakeslicelen //go:linkname panicmakeslicecap //go:linkname makeslice +//go:linkname checkMakeSlice //go:linkname makeslice64 //go:linkname growslice //go:linkname slicecopy @@ -91,6 +92,13 @@ func makeslicecopy(et *_type, tolen int, fromlen int, from unsafe.Pointer) unsaf } func makeslice(et *_type, len, cap int) unsafe.Pointer { + mem := checkMakeSlice(et, len, cap) + return mallocgc(mem, et, true) +} + +// checkMakeSlice is called for append(s, make([]T, len, cap)...) to check +// the values of len and cap. +func checkMakeSlice(et *_type, len, cap int) uintptr { mem, overflow := math.MulUintptr(et.size, uintptr(cap)) if overflow || mem > maxAlloc || len < 0 || len > cap { // NOTE: Produce a 'len out of range' error instead of a @@ -104,8 +112,7 @@ func makeslice(et *_type, len, cap int) unsafe.Pointer { } panicmakeslicecap() } - - return mallocgc(mem, et, true) + return mem } func makeslice64(et *_type, len64, cap64 int64) unsafe.Pointer { -- cgit v1.1