terraform/helper
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
..
acctest Fix swallowed err in acctest package 2017-07-19 16:24:41 -07:00
config
copy
customdiff helper/customdiff: Helper functions for CustomizeDiff 2017-12-18 10:38:20 -08:00
didyoumean helper/didyoumean: helper library for "Did you mean ...? suggestions 2017-10-16 17:50:57 -07:00
diff helper/diff: handle unknownvariablevalue 2016-11-09 14:28:16 -08:00
encryption provider/aws: Extract helper/encryption library 2016-12-06 14:24:21 -06:00
experiment terraform: default shadow to false 2017-02-06 18:02:32 -08:00
hashcode hashcode: "Strings" function for hashing slices of strings 2017-10-20 14:54:23 -07:00
hilmapstructure
logging pretty-print json in http requests/responses 2018-08-09 12:27:58 -07:00
mutexkv
pathorcontents helper/pathorcontents: Skip one test when root 2018-09-19 11:38:51 -05:00
plugin fixup lost collections containing unknowns 2019-03-29 14:54:54 -04:00
resource add (forces new resource) to provider test diffs 2019-03-22 15:30:51 -04:00
schema helper/schema: Prevent setSet() panic with typed nil 2019-04-01 20:10:32 -04:00
shadow Make sure shadow.closeWalker doesn't copy Mutexes 2017-07-07 11:20:54 -04:00
signalwrapper
slowmessage helper/slowmessage 2017-02-14 09:27:23 -08:00
structure helper/structure: More cases for NormalizeJsonString tests 2018-01-17 15:53:38 -08:00
validation helper/validation: Add Any() SchemaValidateFunc 2018-10-18 20:58:53 -04:00
variables command/init: backend-config accepts key=value pairs 2017-03-16 23:27:05 -07:00
wrappedreadline command: split out and tag code so compilation works on Solaris 2016-11-14 00:32:01 -08:00
wrappedstreams helper/wrappedstreams: get original console input/output on Windows 2016-11-21 10:44:01 -08:00
README.md

README.md

Helper Libraries

This folder contains helper libraries for Terraform plugins. A running joke is that this is "Terraform standard library" for plugins. The goal of the packages in this directory are to provide high-level helpers to make it easier to implement the various aspects of writing a plugin for Terraform.