TomWright/dasel

Panic when converting JSON null values

traed opened this issue · 4 comments

traed commented

Describe the bug
Converting JSON to CSV crashes if the JSON file has an array that contains null.

To Reproduce
Steps to reproduce the behavior:

  1. Create a file with:
[
	{
		"a": 1,
		"b": [
			null
		]
	}
]
  1. Run dasel -r json -w csv -f test.json

Expected behavior
null being ignored, converted to an empty string, or a warning being issued

Error message

panic: reflect: call of reflect.Value.Set on zero Value

goroutine 1 [running]:
reflect.flag.mustBeExportedSlow(0x10500eba0?)
	reflect/value.go:235 +0xbc
reflect.flag.mustBeExported(...)
	reflect/value.go:229
reflect.Value.Set({0x10500eba0?, 0x14000261c90?, 0x1?}, {0x0?, 0x0?, 0x104afb3cc?})
	reflect/value.go:2155 +0x84
github.com/tomwright/dasel/v2.derefSlice({0x10500eba0?, 0x14000261c80?, 0x1?})
	github.com/tomwright/dasel/v2/value.go:347 +0x15c
github.com/tomwright/dasel/v2.deref({0x10500eba0?, 0x14000261c80?, 0x10?})
	github.com/tomwright/dasel/v2/value.go:370 +0x68
github.com/tomwright/dasel/v2.derefMap({0x10500eba0?, 0x14000261b90?, 0x1?})
	github.com/tomwright/dasel/v2/value.go:359 +0xf4
github.com/tomwright/dasel/v2.deref({0x10500eba0?, 0x14000261b90?, 0x1?})
	github.com/tomwright/dasel/v2/value.go:372 +0xac
github.com/tomwright/dasel/v2.derefSlice({0x140002713c0?, 0x14000127230?, 0x18?})
	github.com/tomwright/dasel/v2/value.go:347 +0x140
github.com/tomwright/dasel/v2.deref({0x140002713c0?, 0x14000127230?, 0xc600000000000006?})
	github.com/tomwright/dasel/v2/value.go:370 +0x68
github.com/tomwright/dasel/v2.derefValue({{0x140002713c0, 0x14000127230, 0x16}, 0x14000261c10, 0x0, 0x14000231fb0})
	github.com/tomwright/dasel/v2/context.go:101 +0x50
github.com/tomwright/dasel/v2.derefValues({0x140002c4090, 0x1, 0x104e4ff27?})
	github.com/tomwright/dasel/v2/context.go:107 +0xb0
github.com/tomwright/dasel/v2.Select({0x105055480?, 0x140002c4000?}, {0x104e4ff27?, 0x104a9c6d4?})
	github.com/tomwright/dasel/v2/context.go:119 +0x4c
github.com/tomwright/dasel/v2/internal/command.runSelectCommand(0x140002a9ce8, 0x104e76586?)
	github.com/tomwright/dasel/v2/internal/command/select.go:84 +0xdc
github.com/tomwright/dasel/v2/internal/command.selectRunE(0x140002b0000?, {0x1400012c540, 0x0, 0x6?})
	github.com/tomwright/dasel/v2/internal/command/select.go:68 +0x234
github.com/spf13/cobra.(*Command).execute(0x140002b0000, {0x14000134010, 0x6, 0x6})
	github.com/spf13/cobra@v1.6.1/command.go:916 +0x5c8
github.com/spf13/cobra.(*Command).ExecuteC(0x140002b0000)
	github.com/spf13/cobra@v1.6.1/command.go:1044 +0x35c
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.6.1/command.go:968
main.main()
	github.com/tomwright/dasel/v2/cmd/dasel/main.go:10 +0x24

Desktop (please complete the following information):

  • OS: MacOS 12.5.1
  • Version 2.1.1

Thanks for raising this issue. I can replicate it and will take a look

Running the following will also panic with the same error:

echo '' | dasel put -r json -t string -v Frank 'name.first'

It is caused by trying to set the reflect.Value of the nil pointer to slice index.

func derefSlice(value reflect.Value) reflect.Value {
    unpacked := unpackReflectValue(value)
    res := reflect.MakeSlice(unpacked.Type(), unpacked.Len(), unpacked.Len())
    for i := 0; i < unpacked.Len(); i++ {
        res.Index(i).Set(deref(unpacked.Index(i)))
    }
    return res
}

Thanks to @semihbkgr for the fix here #307