hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.71k stars 9.55k forks source link

cidrsubnets() causes crash when expanding out of range list #23841

Closed shmitches closed 4 years ago

shmitches commented 4 years ago

Terraform Version

Terraform v0.12.19

Terraform Configuration Files

If I manually populate the cidrsubnets function with too many values, it will throw an error:


locals{
  base_cidr_block = "172.23.0.0/22"
}

> cidrsubnets(local.base_cidr_block,5,5,5,5,3,3,3,3,4,4,4,4,4,4,4,4)

>
Error: Invalid function argument

  on <console-input> line 1:
  (source code not available)

Invalid value for "newbits" parameter: not enough remaining address space for
a subnet with a prefix of 26 bits after 172.23.3.192/26.

If I store the same values in a list, and expand that list using ... operator, terraform crashes.


locals{
  base_cidr_block = "172.23.0.0/22"
  new_bits = [5,5,5,5,3,3,3,3,4,4,4,4,4,4,4,4]
}

> cidrsubnets(local.base_cidr_block,local.new_bits...)
panic: runtime error: index out of range

Crash Output

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc0003e2c30, 0xc000426560, 0x0, 0xc000595700, 0x1, 0x1, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.2.0/hclsyntax/expression.go:396 +0x4456
github.com/hashicorp/terraform/lang.(*Scope).EvalExpr(0xc0003bb9a0, 0x2377220, 0xc0003e2c30, 0x2378560, 0x351b740, 0x1, 0x0, 0x0, 0x0, 0xc0003e2c30, ...)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/eval.go:95 +0x1b3
github.com/hashicorp/terraform/repl.(*Session).handleEval(0xc000595b30, 0xc00023ce00, 0x34, 0xc00023ce00, 0x34, 0x33, 
0xc00023ce00, 0xc00023ce00)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/repl/session.go:58 +0x22e        
github.com/hashicorp/terraform/repl.(*Session).Handle(0xc000595b30, 0xc00023ce00, 0x34, 0x35, 0xc00023ce00, 0x34, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/repl/session.go:43 +0xec
github.com/hashicorp/terraform/command.(*ConsoleCommand).modeInteractive(0xc000552340, 0xc000595b30, 0x238c5a0, 0xc000508ae0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/command/console_interactive.go:48 +0x19f
github.com/hashicorp/terraform/command.(*ConsoleCommand).Run(0xc000552340, 0xc00000c090, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/command/console.go:148 +0xd61    
github.com/mitchellh/cli.(*CLI).Run(0xc000419400, 0xc000419400, 0xc00022dd90, 0x1)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/mitchellh/cli@v1.0.0/cli.go:255 +0x1f1
main.wrappedMain(0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/main.go:238 +0xc34
main.realMain(0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/main.go:102 +0xb4
main.main()
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/main.go:38 +0x3b

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Expected Behavior

Terraform should throw an error, like manually passing values to the function.

Actual Behavior

Terraform crashes.

danieldreier commented 4 years ago

I've reproduced this on 0.12.25

apparentlymart commented 4 years ago

Hi @shmitches! Thanks for reporting this.

I think this is the same root cause as #22404, so I'm going to close this only to consolidate the discussion there. Thanks again!

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.