From: Davis Goodin Date: Thu, 16 Nov 2023 18:20:56 +0000 (-0800) Subject: cmd/link/internal/loadpe: fix xrels search "not found" detection X-Git-Tag: go1.22rc1~294 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a95c5d37f256d2c3fb462b3e0e544bef39f83f7c;p=gostls13.git cmd/link/internal/loadpe: fix xrels search "not found" detection Fixes findHandlerInXDataAMD64 to handle the return value of sort.Search when the search fails to find anything. Otherwise, the value may later be used as an index, causing an out of range error. Fixes #64200 Change-Id: I4f92e76b3f4d4d5dbe5cbc707f808298c580afe1 Reviewed-on: https://go-review.googlesource.com/c/go/+/543076 TryBot-Result: Gopher Robot Reviewed-by: Carlos Amedee LUCI-TryBot-Result: Go LUCI Run-TryBot: Quim Muntal Reviewed-by: Than McIntosh Reviewed-by: Quim Muntal --- diff --git a/src/cmd/link/internal/loadpe/seh.go b/src/cmd/link/internal/loadpe/seh.go index a97595c10c..0e2cda21dd 100644 --- a/src/cmd/link/internal/loadpe/seh.go +++ b/src/cmd/link/internal/loadpe/seh.go @@ -90,16 +90,17 @@ func findHandlerInXDataAMD64(ldr *loader.Loader, xsym sym.LoaderSym, add int64) // unless it is chained, but we will handle this case later. targetOff := add + unwStaticDataSize*(1+int64(codes)) xrels := ldr.Relocs(xsym) - idx := sort.Search(xrels.Count(), func(i int) bool { + xrelsCount := xrels.Count() + idx := sort.Search(xrelsCount, func(i int) bool { return int64(xrels.At(i).Off()) >= targetOff }) - if idx == 0 { + if idx == xrelsCount { return 0 } if isChained { // The third relocations references the next .xdata entry in the chain, recurse. idx += 2 - if idx >= xrels.Count() { + if idx >= xrelsCount { return 0 } r := xrels.At(idx)