ask := alignUp(npage, pallocChunkPages) * pageSize
totalGrowth := uintptr(0)
- nBase := alignUp(h.curArena.base+ask, physPageSize)
- if nBase > h.curArena.end {
+ // This may overflow because ask could be very large
+ // and is otherwise unrelated to h.curArena.base.
+ end := h.curArena.base + ask
+ nBase := alignUp(end, physPageSize)
+ if nBase > h.curArena.end || /* overflow */ end < h.curArena.base {
// Not enough room in the current arena. Allocate more
// arena space. This may not be contiguous with the
// current arena, so we have to request the full ask.
mSysStatInc(&memstats.heap_released, asize)
mSysStatInc(&memstats.heap_idle, asize)
- // Recalculate nBase
+ // Recalculate nBase.
+ // We know this won't overflow, because sysAlloc returned
+ // a valid region starting at h.curArena.base which is at
+ // least ask bytes in size.
nBase = alignUp(h.curArena.base+ask, physPageSize)
}