From 8287d5da45e4dd6a1c8ddecb739d3a10dc9c958d Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 12 May 2021 17:29:54 +0200 Subject: [PATCH] go/analysis/passes/sigchanyzer: copy to avoid modifying the AST The sigchanyzer pass suggests fixes, but it does that by altering the channel declaration argument. That causes the buildssa.Analyzer fail to fail, along with crashing other passes that depend on it. To fix this, we make a copy of the channel's declaration arguments, and modify the copy instead. Fixes golang/go#46129 Change-Id: I807d36abd49cd3ccc2cc9f907aa98349b2e3231f Reviewed-on: https://go-review.googlesource.com/c/tools/+/319211 Reviewed-by: Emmanuel Odeke Reviewed-by: Cuong Manh Le Run-TryBot: Emmanuel Odeke gopls-CI: kokoro TryBot-Result: Go Bot Trust: Cuong Manh Le --- go/analysis/passes/sigchanyzer/sigchanyzer.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/go/analysis/passes/sigchanyzer/sigchanyzer.go b/go/analysis/passes/sigchanyzer/sigchanyzer.go index b00aa7e1440..0d6c8ebf165 100644 --- a/go/analysis/passes/sigchanyzer/sigchanyzer.go +++ b/go/analysis/passes/sigchanyzer/sigchanyzer.go @@ -59,12 +59,19 @@ func run(pass *analysis.Pass) (interface{}, error) { if chanDecl == nil || len(chanDecl.Args) != 1 { return } - chanDecl.Args = append(chanDecl.Args, &ast.BasicLit{ + + // Make a copy of the channel's declaration to avoid + // mutating the AST. See https://golang.org/issue/46129. + chanDeclCopy := &ast.CallExpr{} + *chanDeclCopy = *chanDecl + chanDeclCopy.Args = append([]ast.Expr(nil), chanDecl.Args...) + chanDeclCopy.Args = append(chanDeclCopy.Args, &ast.BasicLit{ Kind: token.INT, Value: "1", }) + var buf bytes.Buffer - if err := format.Node(&buf, token.NewFileSet(), chanDecl); err != nil { + if err := format.Node(&buf, token.NewFileSet(), chanDeclCopy); err != nil { return } pass.Report(analysis.Diagnostic{