hcl: RelTraversalForExpr must copy traversal before modifying it

The traversal returned from AbsTraversalForExpr may, for some expression
types, be referring to the same backing array as one stored inside the
node itself, and so previously this function may have inadvertently
corrupted the data associated with an AST node.
This commit is contained in:
Martin Atkins 2018-12-14 16:57:21 -08:00
parent dac4796ca1
commit 253da47fd6
1 changed files with 4 additions and 1 deletions

View File

@ -52,11 +52,14 @@ func AbsTraversalForExpr(expr Expression) (Traversal, Diagnostics) {
func RelTraversalForExpr(expr Expression) (Traversal, Diagnostics) {
traversal, diags := AbsTraversalForExpr(expr)
if len(traversal) > 0 {
ret := make(Traversal, len(traversal))
copy(ret, traversal)
root := traversal[0].(TraverseRoot)
traversal[0] = TraverseAttr{
ret[0] = TraverseAttr{
Name: root.Name,
SrcRange: root.SrcRange,
}
return ret, diags
}
return traversal, diags
}