From a95c5d37f256d2c3fb462b3e0e544bef39f83f7c Mon Sep 17 00:00:00 2001 From: Davis Goodin Date: Thu, 16 Nov 2023 10:20:56 -0800 Subject: [PATCH] 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 --- src/cmd/link/internal/loadpe/seh.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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)