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.
This commit is contained in:
Kristin Laemmert 2019-11-15 11:02:59 -05:00 committed by GitHub
parent 7a9fa93724
commit 2c78414724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 5 deletions

View File

@ -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,