From 2c784147245885f8744fae8784fc1638f9040210 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 15 Nov 2019 11:02:59 -0500 Subject: [PATCH] configs/configupgrade: do not panic when int value is out of range (#23394) `terraform 0.12upgrade` assumes that the configuration has passed 0.11 init, but did not explicitly check that the configuration was valid. Certain issues would not get caught because the configuration was syntactically valid. In this case, int or float values out of range resulted in a panic from `Value()`. Since running a 0.11 validate command is a breaking change, this PR merely moves the `Value()` logic for ints and floats into `configupgrade` so the error can be returned to the user, instead of causing a panic. --- configs/configupgrade/upgrade_expr.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/configs/configupgrade/upgrade_expr.go b/configs/configupgrade/upgrade_expr.go index a99aaf5f3..a38ee284d 100644 --- a/configs/configupgrade/upgrade_expr.go +++ b/configs/configupgrade/upgrade_expr.go @@ -38,9 +38,9 @@ Value: return upgradeExpr(tv.Token, filename, interp, an) case hcl1token.Token: - litVal := tv.Value() switch tv.Type { case hcl1token.STRING: + litVal := tv.Value() if !interp { // Easy case, then. printQuotedString(&buf, litVal.(string)) @@ -141,6 +141,7 @@ Value: buf.WriteString(marker) case hcl1token.BOOL: + litVal := tv.Value() if litVal.(bool) { buf.WriteString("true") } else { @@ -148,12 +149,28 @@ Value: } case hcl1token.NUMBER: - num := tv.Value() - buf.WriteString(strconv.FormatInt(num.(int64), 10)) + num, err := strconv.ParseInt(tv.Text, 0, 64) + if err != nil { + diags = diags.Append(&hcl2.Diagnostic{ + Severity: hcl2.DiagError, + Summary: "Invalid number value", + Detail: fmt.Sprintf("Parsing failed: %s", err), + Subject: hcl1PosRange(filename, tv.Pos).Ptr(), + }) + } + buf.WriteString(strconv.FormatInt(num, 10)) case hcl1token.FLOAT: - num := tv.Value() - buf.WriteString(strconv.FormatFloat(num.(float64), 'f', -1, 64)) + num, err := strconv.ParseFloat(tv.Text, 64) + if err != nil { + diags = diags.Append(&hcl2.Diagnostic{ + Severity: hcl2.DiagError, + Summary: "Invalid float value", + Detail: fmt.Sprintf("Parsing failed: %s", err), + Subject: hcl1PosRange(filename, tv.Pos).Ptr(), + }) + } + buf.WriteString(strconv.FormatFloat(num, 'f', -1, 64)) default: // For everything else we'll just pass through the given bytes verbatim,