5
5
package runtime
6
6
7
7
import (
8
+ "runtime/internal/math"
8
9
"runtime/internal/sys"
9
10
"unsafe"
10
11
)
@@ -104,10 +105,11 @@ func growslice(et *_type, old slice, cap int) slice {
104
105
msanread (old .array , uintptr (old .len * int (et .size )))
105
106
}
106
107
108
+ if cap < old .cap {
109
+ panic (errorString ("growslice: cap out of range" ))
110
+ }
111
+
107
112
if et .size == 0 {
108
- if cap < old .cap {
109
- panic (errorString ("growslice: cap out of range" ))
110
- }
111
113
// append should not create a slice with nil pointer but non-zero len.
112
114
// We assume that append doesn't need to preserve old.array in this case.
113
115
return slice {unsafe .Pointer (& zerobase ), old .len , cap }
@@ -169,15 +171,14 @@ func growslice(et *_type, old slice, cap int) slice {
169
171
default :
170
172
lenmem = uintptr (old .len ) * et .size
171
173
newlenmem = uintptr (cap ) * et .size
172
- capmem = roundupsize ( uintptr (newcap ) * et . size )
173
- overflow = uintptr ( newcap ) > maxSliceCap ( et . size )
174
+ capmem , overflow = math . MulUintptr ( et . size , uintptr (newcap ))
175
+ capmem = roundupsize ( capmem )
174
176
newcap = int (capmem / et .size )
175
177
}
176
178
177
- // The check of overflow (uintptr(newcap) > maxSliceCap(et.size))
178
- // in addition to capmem > _MaxMem is needed to prevent an overflow
179
- // which can be used to trigger a segfault on 32bit architectures
180
- // with this example program:
179
+ // The check of overflow in addition to capmem > maxAlloc is needed
180
+ // to prevent an overflow which can be used to trigger a segfault
181
+ // on 32bit architectures with this example program:
181
182
//
182
183
// type T [1<<27 + 1]int64
183
184
//
@@ -188,7 +189,7 @@ func growslice(et *_type, old slice, cap int) slice {
188
189
// s = append(s, d, d, d, d)
189
190
// print(len(s), "\n")
190
191
// }
191
- if cap < old . cap || overflow || capmem > maxAlloc {
192
+ if overflow || capmem > maxAlloc {
192
193
panic (errorString ("growslice: cap out of range" ))
193
194
}
194
195
0 commit comments