From 25f5044e46f5d2c9e870e37c95a13bf01c63a6b7 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Mon, 21 Oct 2019 23:25:32 +0700 Subject: [PATCH] cmd/compile: hard fail if n.Opt() is not nil in walkCheckPtrArithmetic n.Opt() is used in walkCheckPtrArithmetic to prevent infinite loops. The fact that it's used today because n.Opt() is not used for OCONVNOP during walk.go. If that changes, then it's not safe to repalce it anymore. So doing hard fail if that case happens, the author of new changes will be noticed and must change the usage of n.Opt() inside walkCheckPtrArithmetic, too. Change-Id: Ic7094baa1759c647fc10e82457c19026099a0d47 Reviewed-on: https://go-review.googlesource.com/c/go/+/202497 Run-TryBot: Cuong Manh Le TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/walk.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index 0e780bad6c..2ec279bf37 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -3965,8 +3965,12 @@ func walkCheckPtrArithmetic(n *Node, init *Nodes) *Node { // Calling cheapexpr(n, init) below leads to a recursive call // to walkexpr, which leads us back here again. Use n.Opt to // prevent infinite loops. - if n.Opt() == &walkCheckPtrArithmeticMarker { + if opt := n.Opt(); opt == &walkCheckPtrArithmeticMarker { return n + } else if opt != nil { + // We use n.Opt() here because today it's not used for OCONVNOP. If that changes, + // there's no guarantee that temporarily replacing it is safe, so just hard fail here. + Fatalf("unexpected Opt: %v", opt) } n.SetOpt(&walkCheckPtrArithmeticMarker) defer n.SetOpt(nil)