opentofu/helper/schema
Brian Flad a8e3787afc
helper/schema: Prevent setSet() panic with typed nil
References:

* https://github.com/hashicorp/terraform/issues/14418
* v0.9.5 (original bug report): a59ee0b30e/helper/schema/field_writer_map.go (L311)
* v0.11.12 (Terraform AWS Provider discovery): 057286e522/helper/schema/field_writer_map.go (L343)

When creating flatten functions in Terraform Providers that return *schema.Set, its possible to return a typed `nil`, e.g.

```go
func flattenHeaders(h *cloudfront.Headers) *schema.Set {
	if h.Items != nil {
		return schema.NewSet(schema.HashString, flattenStringList(h.Items))
	}
	return nil
}
```

This previously could cause a panic, e.g.

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x1881911]

goroutine 1325 [running]:
github.com/hashicorp/terraform/helper/schema.(*MapFieldWriter).setSet(0xc00054bf00, 0xc00073efa0, 0x5, 0x5, 0x5828140, 0x0, 0xc0002cea50, 0xc000e996a8, 0xc001026e40)
	/Users/bflad/go/pkg/mod/github.com/hashicorp/terraform@v0.11.12/helper/schema/field_writer_map.go:343 +0x211
```

Here we catch the typed `nil` and return an empty list flatmap result instead. Unit testing result prior to code update:

```
--- FAIL: TestMapFieldWriter (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
  panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x1777cdc]

goroutine 913 [running]:
testing.tRunner.func1(0xc00045b800)
  /usr/local/Cellar/go/1.12.1/libexec/src/testing/testing.go:830 +0x392
panic(0x192cf20, 0x2267ca0)
  /usr/local/Cellar/go/1.12.1/libexec/src/runtime/panic.go:522 +0x1b5
github.com/hashicorp/terraform/helper/schema.(*MapFieldWriter).setSet(0xc0004648a0, 0xc0004408d0, 0x1, 0x1, 0x19e3de0, 0x0, 0xc00045c600, 0x30, 0x19e0080)
  /Users/bflad/src/github.com/hashicorp/terraform/helper/schema/field_writer_map.go:344 +0x68c
github.com/hashicorp/terraform/helper/schema.(*MapFieldWriter).set(0xc0004648a0, 0xc0004408d0, 0x1, 0x1, 0x19e3de0, 0x0, 0x1, 0x18)
  /Users/bflad/src/github.com/hashicorp/terraform/helper/schema/field_writer_map.go:107 +0x28b
github.com/hashicorp/terraform/helper/schema.(*MapFieldWriter).WriteField(0xc0004648a0, 0xc0004408d0, 0x1, 0x1, 0x19e3de0, 0x0, 0x0, 0x0)
  /Users/bflad/src/github.com/hashicorp/terraform/helper/schema/field_writer_map.go:89 +0x504
github.com/hashicorp/terraform/helper/schema.TestMapFieldWriter(0xc00045b800)
  /Users/bflad/src/github.com/hashicorp/terraform/helper/schema/field_writer_map_test.go:337 +0x2ddd
testing.tRunner(0xc00045b800, 0x1a44f90)
  /usr/local/Cellar/go/1.12.1/libexec/src/testing/testing.go:865 +0xc0
created by testing.(*T).Run
  /usr/local/Cellar/go/1.12.1/libexec/src/testing/testing.go:916 +0x35a
```
2019-04-01 20:10:32 -04:00
..
as_single_fixup_test.go helper/schema: Implementation of the AsSingle mechanism 2019-03-14 15:36:15 -07:00
as_single_fixup.go helper/schema: Implementation of the AsSingle mechanism 2019-03-14 15:36:15 -07:00
backend_test.go don't panic of the users aborts backend input 2019-03-01 18:45:06 -05:00
backend.go don't panic of the users aborts backend input 2019-03-01 18:45:06 -05:00
core_schema_test.go helper/plugin: Implement Schema.SkipCoreTypeCheck 2019-03-21 15:19:59 -07:00
core_schema.go helper/plugin: Implement Schema.SkipCoreTypeCheck 2019-03-21 15:19:59 -07:00
data_source_resource_shim.go Deprecated -> DeprecationMessage. 2018-06-20 11:21:46 -07:00
equal.go helper/schema: use interface for equality check 2015-01-16 09:32:15 -08:00
field_reader_config_test.go helper/schema: handle TypeMap elem consistently with other collection types 2018-03-14 14:50:41 -07:00
field_reader_config.go helper/schema: handle TypeMap elem consistently with other collection types 2018-03-14 14:50:41 -07:00
field_reader_diff_test.go helper/schema: handle TypeMap elem consistently with other collection types 2018-03-14 14:50:41 -07:00
field_reader_diff.go don't modify argument slices 2018-12-10 11:59:27 -05:00
field_reader_map_test.go helper/schema: handle TypeMap elem consistently with other collection types 2018-03-14 14:50:41 -07:00
field_reader_map.go don't modify argument slices 2018-12-10 11:59:27 -05:00
field_reader_multi_test.go Revert "helper/schema: Make nested Set(s) in List(s) work" (#7436) 2016-06-30 10:48:52 -05:00
field_reader_multi.go helper/schema: full object test for addrToSchema 2015-01-09 17:43:44 -08:00
field_reader_test.go helper/schema: handle TypeMap elem consistently with other collection types 2018-03-14 14:50:41 -07:00
field_reader.go helper/schema: handle TypeMap elem consistently with other collection types 2018-03-14 14:50:41 -07:00
field_writer_map_test.go helper/schema: Prevent setSet() panic with typed nil 2019-04-01 20:10:32 -04:00
field_writer_map.go helper/schema: Prevent setSet() panic with typed nil 2019-04-01 20:10:32 -04:00
field_writer.go helper/schema: FieldWriter, replace Set 2015-01-10 11:44:26 -08:00
getsource_string.go Update various files for new version of "stringer" 2017-12-11 13:26:29 -08:00
provider_test.go decode legacy timeouts 2019-01-30 16:10:17 -05:00
provider.go Copy TF version to helper/schema provider 2019-01-28 14:38:49 -05:00
provisioner_test.go Refactor the provisioner validation function (#15273) 2017-06-15 19:57:04 +02:00
provisioner.go create a SimpleDiff for the new provider shims 2018-10-16 19:14:11 -07:00
README.md helper/schema: README 2014-08-17 20:51:09 -07:00
resource_data_get_source.go helper/schema: diff with set going to 0 elements removes it from state 2015-02-17 11:38:56 -08:00
resource_data_test.go make sure id really gets set in SetId 2019-01-10 20:28:11 -05:00
resource_data.go helper/schema: Add deprecation to ResourceData.UnsafeSetFieldRaw 2019-02-13 22:12:10 -05:00
resource_diff_test.go helper/schema: Always propagate NewComputed for previously zero value primative type attributes 2018-12-04 22:48:30 -05:00
resource_diff.go grammatical updates to comments and docs (#20195) 2019-03-21 14:05:41 -07:00
resource_importer.go helper/schema: pass through import state func 2016-05-16 10:03:57 -07:00
resource_test.go helper/schema: Fix timeout parsing in ResourceTimeout.ConfigDecode 2018-11-05 12:42:12 +00:00
resource_timeout_test.go decode legacy timeouts 2019-01-30 16:10:17 -05:00
resource_timeout.go decode legacy timeouts 2019-01-30 16:10:17 -05:00
resource.go helper/schema: Implementation of the AsSingle mechanism 2019-03-14 15:36:15 -07:00
schema_test.go helper/schema: Schema.AsSingle flag 2019-03-14 15:36:15 -07:00
schema.go helper/schema: Schema.SkipCoreTypeCheck flag 2019-03-21 15:19:59 -07:00
serialize_test.go Add test for TypeMap in a Schema 2016-06-09 16:00:33 -04:00
serialize.go core: Avoid crash on empty TypeSet blocks (#14305) 2017-05-09 20:45:53 +02:00
set_test.go helper/schema: More tests for Set.HashEqual 2017-08-15 21:56:01 -07:00
set.go don't allow computed set elems to be equal 2019-02-05 12:08:17 -05:00
shims_test.go providers: Consistently use int64 for schema versions 2018-11-30 11:22:39 -08:00
shims.go helper/schema: Implementation of the AsSingle mechanism 2019-03-14 15:36:15 -07:00
testing.go create a SimpleDiff for the new provider shims 2018-10-16 19:14:11 -07:00
valuetype_string.go Update various files for new version of "stringer" 2017-12-11 13:26:29 -08:00
valuetype.go helper/schema: zero value of a set should be empty 2015-02-17 16:58:47 -08:00

Terraform Helper Lib: schema

The schema package provides a high-level interface for writing resource providers for Terraform.

If you're writing a resource provider, we recommend you use this package.

The interface exposed by this package is much friendlier than trying to write to the Terraform API directly. The core Terraform API is low-level and built for maximum flexibility and control, whereas this library is built as a framework around that to more easily write common providers.