fix(decode): terminate the schema walk on a self-embedded pointer struct
Assisted-by: GLM 5.3
This commit is contained in:
@@ -538,6 +538,34 @@ func TestUnmarshalEmbeddedPointerStruct(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A struct embedding a pointer to itself is legal Go; decoding into it must
|
||||
// terminate. The schema walk used to recurse through the embedded type
|
||||
// forever.
|
||||
func TestUnmarshalSelfEmbeddedPointerStructTerminates(t *testing.T) {
|
||||
type SelfLink struct {
|
||||
*SelfLink
|
||||
X int `toml:"x"`
|
||||
Y string `toml:"y"`
|
||||
}
|
||||
var n SelfLink
|
||||
if err := Unmarshal([]byte("x = 1\ny = \"s\"\n"), &n); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if n.X != 1 || n.Y != "s" {
|
||||
t.Fatalf("decoded: %+v", n)
|
||||
}
|
||||
|
||||
// A nil self pointer on the encode side stays skippable, as any nil
|
||||
// embedded pointer is.
|
||||
out, err := Marshal(SelfLink{X: 2})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
if want := "x = 2\ny = \"\"\n"; string(out) != want {
|
||||
t.Errorf("output mismatch:\ngot: %q\nwant: %q", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
type RoundTripExtra map[string]int
|
||||
|
||||
type RoundTripMapCfg struct {
|
||||
|
||||
Reference in New Issue
Block a user