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

metadata.Keys (and thus metadata.Undecoded) is invalid in some cases #417

Open
maroux opened this issue Jul 9, 2024 · 2 comments · May be fixed by #418
Open

metadata.Keys (and thus metadata.Undecoded) is invalid in some cases #417

maroux opened this issue Jul 9, 2024 · 2 comments · May be fixed by #418

Comments

@maroux
Copy link

maroux commented Jul 9, 2024

Sample code:

package main

import (
	"fmt"

	"github.com/BurntSushi/toml"
)

func main() {
	content := `[table.subtable.subsubtable]
a = 1
b = 2
`
	var v interface{}
	metadata, err := toml.Decode(content, &v)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", metadata.Keys())
}

Playground: https://go.dev/play/p/Rr5bm5DbEql

Output:

[]toml.Key{toml.Key{"table", "subtable", "subsubtable"}, toml.Key{"table", "subtable", "subsubtable", "b"}, toml.Key{"table", "subtable", "subsubtable", "b"}}

Notice how the second and third value in that slice both point to b.

Expected output:

[]toml.Key{toml.Key{"table", "subtable", "subsubtable"}, toml.Key{"table", "subtable", "subsubtable", "a"}, toml.Key{"table", "subtable", "subsubtable", "b"}}

The root cause seems to be somewhere near this append call. The result of append can return a slice backed by the same array if the capacity is available. See Go example code: https://go.dev/play/p/2FGKuCvCUKz.

@maroux
Copy link
Author

maroux commented Jul 9, 2024

I've opened 2 PRs with 2 possible fixes for this bug.

@im-alex07
Copy link

Output:

[]toml.Key{toml.Key{"table", "subtable", "subsubtable"}, toml.Key{"table", "subtable", "subsubtable", "b"}, toml.Key{"table", "subtable", "subsubtable", "b"}}

Notice how the second and third value in that slice both point to b.

I also encountered this bug. Please fix it.

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