Skip to content

Commit fe70866

Browse files
committed
runtime: throw on zero-sized range passed to addrRanges.add
addrRanges represents a set of addresses. Currently, passing in a zero-sized range will cause that range to be added to the list, even though it doesn't represent any address (addrRanges.contains will still always return false, and findSucc will give surprising results). We could ignore this input, but it's almost always a bug for the calling code to pass in a zero-sized range, so just throw. Change-Id: I8ed09e15b79a3a33e2d0cf5ed55f9e497388e7a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/242817 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Michael Knyszek <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent e01a1c0 commit fe70866

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/runtime/mranges.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (a *addrRanges) contains(addr uintptr) bool {
218218

219219
// add inserts a new address range to a.
220220
//
221-
// r must not overlap with any address range in a.
221+
// r must not overlap with any address range in a and r.size() must be > 0.
222222
func (a *addrRanges) add(r addrRange) {
223223
// The copies in this function are potentially expensive, but this data
224224
// structure is meant to represent the Go heap. At worst, copying this
@@ -229,6 +229,12 @@ func (a *addrRanges) add(r addrRange) {
229229
// of 16) and Go heaps are usually mostly contiguous, so the chance that
230230
// an addrRanges even grows to that size is extremely low.
231231

232+
// An empty range has no effect on the set of addresses represented
233+
// by a, but passing a zero-sized range is almost always a bug.
234+
if r.size() == 0 {
235+
print("runtime: range = {", hex(r.base.addr()), ", ", hex(r.limit.addr()), "}\n")
236+
throw("attempted to add zero-sized address range")
237+
}
232238
// Because we assume r is not currently represented in a,
233239
// findSucc gives us our insertion index.
234240
i := a.findSucc(r.base.addr())

0 commit comments

Comments
 (0)