pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.68k stars 208 forks source link

Panic after unmarshal twice to same object #851

Closed gxsshallot closed 1 year ago

gxsshallot commented 1 year ago

Describe the bug

We need to parse the file content to same object many times. But the library will be panic after another unmarshal operation.

To Reproduce

package main

import (
    "github.com/pelletier/go-toml/v2"
)

type Target struct {
    Params map[string]string `toml:"params"`
}

func main() {
    content := "params = {a=\"1\",b=\"2\"}"
    var target Target
    _ = toml.Unmarshal([]byte(content), &target)
    _ = toml.Unmarshal([]byte(content), &target)
}

Run the test program got the panic:

panic: reflect.Value.SetMapIndex: value of type interface {} is not assignable to type string

goroutine 1 [running]:
reflect.Value.assignTo({0x4c76a0?, 0xc0000a62e0?, 0xc0000a62e0?}, {0x4dcb68, 0x19}, 0x4c5760, 0x0)
        /opt/go-1.19.6/src/reflect/value.go:3145 +0x2a5
reflect.Value.SetMapIndex({0x4c8a20?, 0xc0000b6018?, 0x0?}, {0x4c5760, 0xc0000a62c0, 0x98}, {0x4c76a0, 0xc0000a62e0, 0x194})
        /opt/go-1.19.6/src/reflect/value.go:2295 +0x1b6
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValuePart(0xc0000c0080, {0x1, 0xc0000c6460}, 0xc0000a2990?, {0x4c8a20?, 0xc0000b6018?, 0x4d5800?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:1058 +0x5fa
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValueInner(0xc0000bc043?, {0x1, 0xc0000c6460}, 0x1?, {0x4c8a20?, 0xc0000b6018?, 0x195?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:1000 +0x71
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValue(0xc0000c0080, 0xc0000c6428, {0x4c8a20?, 0xc0000b6018?, 0x4b72a8?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:986 +0xa5
github.com/pelletier/go-toml/v2.(*decoder).unmarshalInlineTable(0xc0000c0080?, 0xc0000c63f0, {0x4c8a20?, 0xc0000b6018?, 0x4c8660?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:756 +0x2bc
github.com/pelletier/go-toml/v2.(*decoder).handleValue(0x0?, 0xc0000c63f0, {0x4c8a20?, 0xc0000b6018?, 0x28?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:653 +0xe8
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValueInner(0x4cab00?, {0x1, 0x0}, 0xc0000ce008?, {0x4c8a20?, 0xc0000b6018?, 0xc0000bc040?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:1004 +0x8c
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValuePart(0xc0000c0080, {0x1, 0xc0000c63b8}, 0x5950e0?, {0x4cab00?, 0xc0000b6018?, 0xffffffffffffffff?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:1075 +0x9cc
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValueInner(0x6?, {0x1, 0xc0000c63b8}, 0xc0000a2d90?, {0x4cab00?, 0xc0000b6018?, 0xc0000c6380?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:1000 +0x71
github.com/pelletier/go-toml/v2.(*decoder).handleKeyValue(0xc0000c0080, 0xc0000c6380, {0x4cab00?, 0xc0000b6018?, 0x18?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:986 +0xa5
github.com/pelletier/go-toml/v2.(*decoder).handleRootExpression(0xc0000c0080, 0xc0000c6380, {0x4cab00?, 0xc0000b6018?, 0x0?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:258 +0xa9
github.com/pelletier/go-toml/v2.(*decoder).fromParser(0xc0000c0080, {0x4cab00?, 0xc0000b6018?, 0x5950e0?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:224 +0xaa
github.com/pelletier/go-toml/v2.(*decoder).FromParser(0xc0000c0080, {0x4c2960?, 0xc0000b6018?})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:209 +0x1e7
github.com/pelletier/go-toml/v2.Unmarshal({0xc0000bc030, 0x16, 0x18}, {0x4c2960, 0xc0000b6018})
        /home/shallot/.go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.6/unmarshaler.go:28 +0x11a
main.main()
        /home/shallot/project/common/micro/main1.go:15 +0x76
exit status 2

Expected behavior

Not panic.

Versions

Additional context

No.

apprehensions commented 1 year ago

facing this same issue

pelletier commented 1 year ago

Thanks for reporting this bug! I've merged https://github.com/pelletier/go-toml/pull/854, which should fix the problem. Feel free to reopen this issue if it doesn't!