From c101504c2daa6b6f0bfd84ccac7c077e36970717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 27 Jul 2020 23:14:25 +0100 Subject: [PATCH] avoid nil pointer dereference panics in enclosingFrame This should make the code more resilient; the crash was encountered in some complex sites, but we were unable to reproduce it with a small HTML test page. The current best theory is that a node was used after it's replaced, so its parent is no longer present in the frame's Nodes map. In that case, return an empty FrameID, and the selector will try again with the new and correct node. --- target.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/target.go b/target.go index 9887fa9c..1856edb2 100644 --- a/target.go +++ b/target.go @@ -47,7 +47,15 @@ func (t *Target) enclosingFrame(node *cdp.Node) cdp.FrameID { t.frameMu.RLock() top := t.frames[t.cur] t.frameMu.RUnlock() - for node.FrameID == "" { + for { + if node == nil { + // Avoid crashing. This can happen if we're using an old + // node that has been replaced, for example. + return "" + } + if node.FrameID != "" { + break + } node = top.Nodes[node.ParentID] } return node.FrameID