@@ -19,6 +19,7 @@ package runtime
19
19
20
20
import (
21
21
"runtime/internal/atomic"
22
+ "runtime/internal/math"
22
23
"unsafe"
23
24
)
24
25
@@ -78,7 +79,8 @@ func makechan(t *chantype, size int) *hchan {
78
79
throw ("makechan: bad alignment" )
79
80
}
80
81
81
- if size < 0 || uintptr (size ) > maxSliceCap (elem .size ) || uintptr (size )* elem .size > maxAlloc - hchanSize {
82
+ mem , overflow := math .MulUintptr (elem .size , uintptr (size ))
83
+ if overflow || mem > maxAlloc - hchanSize || size < 0 {
82
84
panic (plainError ("makechan: size out of range" ))
83
85
}
84
86
@@ -88,20 +90,20 @@ func makechan(t *chantype, size int) *hchan {
88
90
// TODO(dvyukov,rlh): Rethink when collector can move allocated objects.
89
91
var c * hchan
90
92
switch {
91
- case size == 0 || elem . size == 0 :
93
+ case mem == 0 :
92
94
// Queue or element size is zero.
93
95
c = (* hchan )(mallocgc (hchanSize , nil , true ))
94
96
// Race detector uses this location for synchronization.
95
97
c .buf = c .raceaddr ()
96
98
case elem .kind & kindNoPointers != 0 :
97
99
// Elements do not contain pointers.
98
100
// Allocate hchan and buf in one call.
99
- c = (* hchan )(mallocgc (hchanSize + uintptr ( size ) * elem . size , nil , true ))
101
+ c = (* hchan )(mallocgc (hchanSize + mem , nil , true ))
100
102
c .buf = add (unsafe .Pointer (c ), hchanSize )
101
103
default :
102
104
// Elements contain pointers.
103
105
c = new (hchan )
104
- c .buf = mallocgc (uintptr ( size ) * elem . size , elem , true )
106
+ c .buf = mallocgc (mem , elem , true )
105
107
}
106
108
107
109
c .elemsize = uint16 (elem .size )
0 commit comments