From 44532f1a9defb9cc4f48e74a7fa1ffd1dfa67022 Mon Sep 17 00:00:00 2001 From: Marvin Stenger Date: Thu, 24 Mar 2016 01:55:42 +0100 Subject: [PATCH] runtime: fix inconsistency in slice.go Fixes #14938. Additionally some simplifications along the way. Change-Id: I2c5fb7e32dcc6fab68fff36a49cb72e715756abe Reviewed-on: https://go-review.googlesource.com/21046 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor --- src/runtime/slice.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/runtime/slice.go b/src/runtime/slice.go index 5e88ed9453..d35ecadb16 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -62,23 +62,20 @@ func growslice(t *slicetype, old slice, cap int) slice { } newcap := old.cap - if newcap+newcap < cap { + doublecap := newcap + newcap + if cap > doublecap { newcap = cap } else { - for { - if old.len < 1024 { - newcap += newcap - } else { + if old.len < 1024 { + newcap = doublecap + } else { + for newcap < cap { newcap += newcap / 4 } - if newcap >= cap { - break - } } - } - - if uintptr(newcap) >= maxcap { - panic(errorString("growslice: cap out of range")) + if uintptr(newcap) > maxcap { + panic(errorString("growslice: cap out of range")) + } } lenmem := uintptr(old.len) * et.size -- 2.48.1