Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic if decoding to an empty slice #83

Closed
chowey opened this issue May 25, 2015 · 0 comments
Closed

panic if decoding to an empty slice #83

chowey opened this issue May 25, 2015 · 0 comments

Comments

@chowey
Copy link

chowey commented May 25, 2015

Try the following code:

package main

import "github.com/BurntSushi/toml"
import "log

type Q struct {
    Test []string
}

func main() {
    q := Q{Test: []string{}}
    _, err := toml.Decode(`Test = ["test"]`, &q)
    if err != nil {
        log.Fatal(err)
    }
}

This results in a panic:

panic: reflect: slice index out of range

goroutine 1 [running]:
reflect.Value.Index(0x6c1b60, 0x12658d80, 0xd7, 0x0, 0x0, 0x0, 0x0)
        c:/go/src/reflect/value.go:818 +0x10f github.com/BurntSushi/toml.(*MetaData).unifySliceArray(0x12652ba0, 0x6c0d60, 0x12658dd0, 0x57, 0x6c1b60, 0x12658d80, 0xd7, 0x0, 0x0)
        %GOPATH%/src/github.com/BurntSushi/toml/decode.go:321 +0xde github.com/BurntSushi/toml.(*MetaData).unifySlice(0x12652ba0, 0x6c0d60, 0x12658dd0, 0x6c1b60, 0x12658d80, 0xd7, 0x0, 0x0)
        %GOPATH%/src/github.com/BurntSushi/toml/decode.go:314 +0x1b4
github.com/BurntSushi/toml.(*MetaData).unify(0x12652ba0, 0x6c0d60, 0x12658dd0, 0x6c1b60, 0x12658d80, 0xd7, 0x0, 0x0)
        %GOPATH%/src/github.com/BurntSushi/toml/decode.go:206 +0xb11 github.com/BurntSushi/toml.(*MetaData).unifyStruct(0x12652ba0, 0x6cc160, 0x126543c0, 0x6d3e80, 0x12658d80, 0xd9, 0x0, 0x0)
        %GOPATH%/src/github.com/BurntSushi/toml/decode.go:252 +0x65a github.com/BurntSushi/toml.(*MetaData).unify(0x12652ba0, 0x6cc160, 0x126543c0, 0
x6d3e80, 0x12658d80, 0xd9, 0x0, 0x0)
        %GOPATH%/src/github.com/BurntSushi/toml/decode.go:200 +0xb9f github.com/BurntSushi/toml.Decode(0x7adf28, 0xf, 0x6bcbe0, 0x12658d80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        %GOPATH%/src/github.com/BurntSushi/toml/decode.go:114 +0x1c5 main.NewStaticContext(0x12655947, 0xa, 0x12656900, 0x0, 0x0, 0x0)
        ...

This is only caused when the Test field is initialized to an empty slice.

If Test is nil or has at least one element, Decode works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant