aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2021-05-03 13:24:27 -0400
committerDavid Chase <drchase@google.com>2021-05-03 18:17:42 +0000
commit8a4b7294af226f2628edc0ef2a7c45ecff6e06ff (patch)
tree9aa2102a65a6579b507d9b31aeb63cefb26a9d89 /src/runtime
parent7f9febd4a19a02bd25e40c63289268e9c062701e (diff)
downloadgo-8a4b7294af226f2628edc0ef2a7c45ecff6e06ff.tar.gz
go-8a4b7294af226f2628edc0ef2a7c45ecff6e06ff.zip
cmd/compile: fix possible nil deref added in CL 270943
In the event allocSpan returned a nil, this would crash. Cleaned up the code and comments slightly, too. Change-Id: I6231d4b4c14218e6956b4a97a205adc3206f59ec Reviewed-on: https://go-review.googlesource.com/c/go/+/316429 Trust: David Chase <drchase@google.com> Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/mheap.go16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/runtime/mheap.go b/src/runtime/mheap.go
index 952c0b21b0..84c00ce8f8 100644
--- a/src/runtime/mheap.go
+++ b/src/runtime/mheap.go
@@ -897,7 +897,8 @@ func (s spanAllocType) manual() bool {
// spanclass indicates the span's size class and scannability.
//
// If needzero is true, the memory for the returned span will be zeroed.
-// The boolean returned indicates whether the returned span is zeroed.
+// The boolean returned indicates whether the returned span contains zeroes,
+// either because this was requested, or because it was already zeroed.
func (h *mheap) alloc(npages uintptr, spanclass spanClass, needzero bool) (*mspan, bool) {
// Don't do any operations that lock the heap on the G stack.
// It might trigger stack growth, and the stack growth code needs
@@ -912,14 +913,15 @@ func (h *mheap) alloc(npages uintptr, spanclass spanClass, needzero bool) (*mspa
s = h.allocSpan(npages, spanAllocHeap, spanclass)
})
+ if s == nil {
+ return nil, false
+ }
isZeroed := s.needzero == 0
- if s != nil {
- if needzero && s.needzero != 0 {
- memclrNoHeapPointers(unsafe.Pointer(s.base()), s.npages<<_PageShift)
- isZeroed = true
- }
- s.needzero = 0
+ if needzero && !isZeroed {
+ memclrNoHeapPointers(unsafe.Pointer(s.base()), s.npages<<_PageShift)
+ isZeroed = true
}
+ s.needzero = 0
return s, isZeroed
}