diff --git a/internal/api/decode_body.go b/internal/api/decode_body.go index 3b70911a..b5193d6f 100644 --- a/internal/api/decode_body.go +++ b/internal/api/decode_body.go @@ -2,11 +2,16 @@ package api import ( "bytes" + "cmp" "encoding/json" "errors" "net/http" + "reflect" + "slices" "strconv" "strings" + "sync" + "unicode" "unicode/utf8" ) @@ -22,9 +27,12 @@ const maxDecodeWork = 256 << 10 // of the wrong JSON type. encoding/json keeps decoding after a type error // but returns only the first one. unmarshalBody replaces the member that // failed by null, which every type accepts as no change, and decodes again -// until the body decodes or a bound is reached. A body with one wrong-type -// field returns the error of encoding/json unchanged. Any other error -// (syntax, a wrong top-level type) is returned unchanged. +// until the body decodes or a bound is reached. It also stops when null +// does not fix the field (a map key that does not decode, an UnmarshalJSON +// that refuses null): when a pass leaves the body as it was, or the next +// decode names the same field with the same type again. A body with one +// wrong-type field returns the error of encoding/json unchanged. Any other +// error (syntax, a wrong top-level type) is returned unchanged. func unmarshalBody(body []byte, dst any) error { err := json.Unmarshal(body, dst) te, ok := errors.AsType[*json.UnmarshalTypeError](err) @@ -35,14 +43,15 @@ func unmarshalBody(body []byte, dst any) error { errs.add(te) work, budget := body, maxDecodeWork-len(body) // the first decode counts for !errs.full() && budget >= len(work) { - next, ok := nullMembers(work, te.Field) + next, ok := nullMembers(work, reflect.TypeOf(dst), te) // null is longer than a value of 1 to 3 bytes, so the body can grow. - if !ok || len(next) > budget { + if !ok || len(next) > budget || bytes.Equal(next, work) { break } work, budget = next, budget-len(next) + prev := te te, ok = errors.AsType[*json.UnmarshalTypeError](json.Unmarshal(work, dst)) - if !ok || te.Field == "" { + if !ok || te.Field == "" || te.Field == prev.Field && te.Type == prev.Type { break } errs.add(te) @@ -149,60 +158,286 @@ func unknownFieldText(err error) string { return "unknown field " + strconv.Quote(name[:cut]+"…") + " (" + strconv.Itoa(len(name)) + " bytes)" } -// nullMembers returns body with every value at the dotted path field -// (members by name, array elements by index) replaced by null. It reports -// false when no value is found at the path. -func nullMembers(body []byte, field string) ([]byte, bool) { - spans := memberSpans(body, strings.Split(field, ".")) - if len(spans) == 0 { +// nullMembers returns body with every value that the type error te names +// replaced by null; t is the type that body decodes into. It reports false +// when it finds no such value. +func nullMembers(body []byte, t reflect.Type, te *json.UnmarshalTypeError) ([]byte, bool) { + w := spanWalk{body: body, dec: json.NewDecoder(bytes.NewReader(body))} + if w.value(t, strings.Split(te.Field, ".")) != nil || len(w.spans) == 0 { return nil, false } + // A map key that holds "." gives two values one path (the keys "a.b" + // and "a" then "b"): the one that te names has the type it names. When + // none has it (te names a map key, or comes from an UnmarshalJSON), + // every value at the path is null. + if slices.ContainsFunc(w.spans, func(s valueSpan) bool { return s.typ == te.Type }) { + w.spans = slices.DeleteFunc(w.spans, func(s valueSpan) bool { return s.typ != te.Type }) + } var out bytes.Buffer prev := 0 - for _, sp := range spans { - out.Write(body[prev:sp[0]]) + for _, sp := range w.spans { + out.Write(body[prev:sp.start]) out.WriteString("null") - prev = sp[1] + prev = sp.end } out.Write(body[prev:]) return out.Bytes(), true } -// memberSpans returns the byte ranges of the values at path in the JSON -// value b. encoding/json matches a member name to a field without regard to -// case, and so does this. -func memberSpans(b []byte, path []string) [][2]int { - dec := json.NewDecoder(bytes.NewReader(b)) - open, err := dec.Token() - if err != nil { - return nil - } - var spans [][2]int - for index := 0; dec.More(); index++ { - name := strconv.Itoa(index) - if open == json.Delim('{') { - key, err := dec.Token() - if err != nil { - return nil +// valueSpan is the byte range of a value in a body, and the type (without +// pointers) that the value decodes into. +type valueSpan struct { + start, end int + typ reflect.Type +} + +// spanWalk finds the values at a path in one pass over a body, following +// the Go type that the body decodes into. The path is an +// UnmarshalTypeError.Field split at each ".": encoding/json spells the path +// of a value as its JSON Pointer (RFC 6901) with each "/" written as ".", so +// a member is its name with "~" as "~0" and "/" as "~1", and an array +// element is its index. A name takes one segment more than it holds ".". A +// member of a struct is the field that encoding/json decodes it into (see +// fieldFor), and members that differ in case can be one field; a member +// that is no field is on the path only as a key of the struct's `,embed` +// map (see bodyFields). A member of a map is its exact key. +// A type with its own UnmarshalJSON is walked as its fields, which is right +// for a method that decodes into them (wrefCreateBranchRequest). +type spanWalk struct { + body []byte + dec *json.Decoder + spans []valueSpan +} + +// value reads the next value of the body, which decodes into t, and adds +// the span of each value at path in it. +func (w *spanWalk) value(t reflect.Type, path []string) error { + for t.Kind() == reflect.Pointer { + t = t.Elem() + } + // The value starts after the white space, ":" or "," that follow the + // last token. + start := len(w.body) - len(bytes.TrimLeft(w.body[w.dec.InputOffset():], " \t\r\n:,")) + if len(path) == 0 { + err := w.skip() + w.spans = append(w.spans, valueSpan{start, int(w.dec.InputOffset()), t}) + return err + } + switch k := t.Kind(); { + case start == len(w.body): + case w.body[start] == '{' && (k == reflect.Struct || k == reflect.Map): + return w.members(t, path) + case w.body[start] == '[' && (k == reflect.Slice || k == reflect.Array): + return w.elements(t.Elem(), path) + } + return w.skip() +} + +// members reads an object that decodes into the struct or map t. +func (w *spanWalk) members(t reflect.Type, path []string) error { + st := structFields{rest: t} // a map takes every member + if t.Kind() == reflect.Struct { + st = bodyFields(t) + } + if _, err := w.dec.Token(); err != nil { + return err + } + for w.dec.More() { + tok, err := w.dec.Token() + if err != nil { + return err + } + name, _ := tok.(string) + n := strings.Count(name, ".") + 1 + var next reflect.Type // nil: the member is not on the path + if n <= len(path) { + seg := strings.Join(path[:n], ".") + if i := fieldFor(st.fields, name); i >= 0 { + if i == fieldFor(st.fields, strings.ReplaceAll(strings.ReplaceAll(seg, "~1", "/"), "~0", "~")) { + next = st.fields[i].typ + } + } else if st.rest != nil && st.rest.Kind() == reflect.Map && strings.ReplaceAll(strings.ReplaceAll(name, "~", "~0"), "/", "~1") == seg { + next = st.rest.Elem() } - name, _ = key.(string) } - var raw json.RawMessage - if dec.Decode(&raw) != nil { - return nil + if next == nil { + err = w.skip() + } else { + err = w.value(next, path[n:]) } - end := int(dec.InputOffset()) - start := end - len(raw) - if !strings.EqualFold(name, path[0]) { - continue + if err != nil { + return err } - if len(path) == 1 { - spans = append(spans, [2]int{start, end}) - continue + } + _, err := w.dec.Token() + return err +} + +// elements reads an array whose elements decode into t. +func (w *spanWalk) elements(t reflect.Type, path []string) error { + if _, err := w.dec.Token(); err != nil { + return err + } + for i := 0; w.dec.More(); i++ { + var err error + if strconv.Itoa(i) == path[0] { + err = w.value(t, path[1:]) + } else { + err = w.skip() } - for _, sp := range memberSpans(raw, path[1:]) { - spans = append(spans, [2]int{start + sp[0], start + sp[1]}) + if err != nil { + return err } } - return spans + _, err := w.dec.Token() + return err +} + +// skip reads the next value of the body. +func (w *spanWalk) skip() error { + var raw json.RawMessage + return w.dec.Decode(&raw) +} + +// bodyField is a field of a struct type that encoding/json decodes: its +// JSON name, its type, its depth (more than 0 for a field of an embedded +// struct), whether its json tag gives the name, and whether the name +// matches in its own case only (the tag option case:strict). +type bodyField struct { + name string + typ reflect.Type + depth int + tagged, strict bool +} + +// structFields is what bodyFields returns for a struct type. +type structFields struct { + fields []bodyField + rest reflect.Type // the `,embed` map or jsontext.Value that takes the members that no field takes, or nil +} + +// bodyFields returns the fields of the struct type t that encoding/json +// decodes, in breadth-first order, by the rules of encoding/json (on +// json/v2, as Go 1.27 builds it): a struct or a pointer to one that is +// embedded without a JSON name, or has the option embed and no name, adds +// its fields one level deeper, and of the fields with one name only the +// shallowest one counts, or at one depth the only one whose json tag gives +// the name. Of the fields that take the members that no field takes (see +// takesRest), the shallowest one does, unless two share its depth. +func bodyFields(t reflect.Type) structFields { + if fields, ok := bodyFieldsCache.Load(t); ok { + return fields.(structFields) + } + type embedded struct { + t reflect.Type + depth int + follow bool // false for a type seen before: its embedded structs are not followed + } + queue := []embedded{{t, 0, true}} + seen := map[reflect.Type]bool{t: true} + var all, rests []bodyField + for i := 0; i < len(queue); i++ { + q := queue[i] + for j := range q.t.NumField() { + sf := q.t.Field(j) + tag := sf.Tag.Get("json") + if tag == "-" || !sf.IsExported() && !sf.Anonymous { + continue + } + name, embed, strict := jsonTag(tag) + ft := sf.Type + if ft.Kind() == reflect.Pointer && ft.Name() == "" { + ft = ft.Elem() + } + embed = name == "" && (embed || sf.Anonymous && ft.Kind() == reflect.Struct) + switch { + case embed && ft.Kind() == reflect.Struct: + if q.follow { + queue = append(queue, embedded{ft, q.depth + 1, !seen[ft]}) + } + seen[ft] = true + case embed && sf.IsExported() && takesRest(ft): + rests = append(rests, bodyField{typ: ft, depth: q.depth}) + case sf.IsExported() || ft.Kind() == reflect.Struct: + all = append(all, bodyField{cmp.Or(name, sf.Name), sf.Type, q.depth, name != "", strict}) + } + } + } + var fields structFields + for i, f := range all { + counts := true + for j, g := range all { + if j != i && g.name == f.name && (g.depth < f.depth || g.depth == f.depth && (g.tagged || !f.tagged)) { + counts = false + break + } + } + if counts { + fields.fields = append(fields.fields, f) + } + } + if len(rests) == 1 || len(rests) > 1 && rests[0].depth < rests[1].depth { + fields.rest = rests[0].typ + } + bodyFieldsCache.Store(t, fields) + return fields +} + +// bodyFieldsCache holds the bodyFields of each struct type. +var bodyFieldsCache sync.Map + +// takesRest reports whether encoding/json gives the members that no field +// takes to an exported field of type t (without pointers) that has the +// option embed and no name: a jsontext.Value takes them as they are, and so +// does a map whose key type is a string kind without marshal methods. Any +// other such field is a plain field. +func takesRest(t reflect.Type) bool { + if t.PkgPath() == "encoding/json/jsontext" && t.Name() == "Value" { + return true + } + return t.Kind() == reflect.Map && t.Key().Kind() == reflect.String && !slices.ContainsFunc(marshalMethods, func(m string) bool { + _, ok := reflect.PointerTo(t.Key()).MethodByName(m) + return ok + }) +} + +// marshalMethods are the methods by which a type marshals or unmarshals +// itself in encoding/json. +var marshalMethods = []string{"MarshalJSON", "MarshalJSONTo", "AppendText", "MarshalText", "UnmarshalJSON", "UnmarshalJSONFrom", "UnmarshalText"} + +// jsonTag returns the JSON name that a json struct tag gives, "" for none, +// and whether its options hold embed, and case:strict without case:ignore, +// as encoding/json reads the tag (parseFieldOptions of json/v2). The name +// ends at the first ",". When a backslash, a quote or a backtick comes +// first, the name is the run of letters, digits and "_" that starts the +// tag, and none when that run is empty or starts with a digit. Invalid +// UTF-8 in the name reads as U+FFFD, as in a member name. +func jsonTag(tag string) (name string, embed, strict bool) { + name, opts, _ := strings.Cut(tag, ",") + if strings.ContainsAny(name, "\\'\"`") { + name = name[:len(name)-len(strings.TrimLeftFunc(name, func(r rune) bool { + return r == '_' || unicode.IsLetter(r) || unicode.IsNumber(r) + }))] + if r, _ := utf8.DecodeRuneInString(name); unicode.IsNumber(r) { + name = "" + } + } + ignore := false + for opt := range strings.SplitSeq(opts, ",") { + embed = embed || opt == "embed" + strict = strict || opt == "case:strict" + ignore = ignore || opt == "case:ignore" + } + return string([]rune(name)), embed, strict && !ignore +} + +// fieldFor returns the index in fields of the field that encoding/json +// decodes a member name into: the field of that name, else the first one +// whose name matches it without regard to case and has no case:strict. It +// returns -1 when no field takes the name. +func fieldFor(fields []bodyField, name string) int { + if i := slices.IndexFunc(fields, func(f bodyField) bool { return f.name == name }); i >= 0 { + return i + } + return slices.IndexFunc(fields, func(f bodyField) bool { return !f.strict && strings.EqualFold(f.name, name) }) } diff --git a/internal/api/decode_body_test.go b/internal/api/decode_body_test.go index 08a6f9e3..e1927bcd 100644 --- a/internal/api/decode_body_test.go +++ b/internal/api/decode_body_test.go @@ -2,8 +2,11 @@ package api import ( "encoding/json" + "encoding/json/jsontext" "errors" "fmt" + "reflect" + "slices" "strings" "testing" ) @@ -137,6 +140,18 @@ func TestUnmarshalBodyBounds(t *testing.T) { if got := invalidParamsFor(unmarshalBody([]byte(long), new(decodeBodyReq))); len(got) != 1 || got[0].Name != "paths" { t.Fatalf("a body of %d bytes with a long first member named %+v, want paths only", len(long), got) } + // A member that is no field is never the wrong-type value, even when its + // name spells the path of one, so nulling it cannot change how many + // fields the work bound lets a body name. + for _, unknown := range []string{`"K":"` + strings.Repeat("x", 60<<10) + `"`, strings.Repeat(`"K":1,`, 499) + `"K":1`} { + named := func(name string) int { + body := `{"paths":[` + strings.Repeat("7,", 149) + `7],` + strings.ReplaceAll(unknown, `"K"`, `"`+name+`"`) + `}` + return len(invalidParamsFor(unmarshalBody([]byte(body), new(decodeBodyReq)))) + } + if got, want := named("paths.0"), named("paths_0"); got != want { + t.Fatalf("with unknown members paths.0 the body named %d items, with paths_0 %d", got, want) + } + } // The first decode counts too: a body of more than 128 KiB names only // its first wrong-type field, and one of 126 KiB names two. for _, tc := range []struct{ entries, want int }{{70_000, 1}, {63_000, 2}} { @@ -212,3 +227,168 @@ func TestDecodeError(t *testing.T) { t.Errorf("frame with a syntax error: %q", got) } } + +// decodePathReq has members that encoding/json names by their JSON +// Pointer with each "/" written as ".": map keys that hold "." (kept as +// is), "/" (written ~1) or "~" (written ~0), and members that null does +// not fix. It has fields of embedded structs, and a field X in two of them, +// which encoding/json drops, so that a member "X" is the field x. Each +// map from Dp on is the field that encoding/json decodes a member into by +// one of its field rules, beside a field of another type that the member +// does not reach: the walk stops when it takes that one. +type decodePathReq struct { + Decodes decodeCount `json:"n"` + M map[string]int `json:"m"` + Nested map[string]map[string]int `json:"nested"` + Keys map[int]int `json:"keys"` + Strict refusesNull `json:"strict"` + Count int `json:"count"` + Dotted int `json:"a.b/c"` + Lower int `json:"x"` + decodeEmbedded + *DecodePtrEmbedded + Dp map[string]int `json:"dp"` + Skip int `json:"-"` + Dash map[string]int `json:"-,"` //nolint:staticcheck // the name "-" + Ex0 int `json:"Ex"` + Ex map[string]int `json:"ex"` + Fo map[string]int `json:"fo"` + Fo1 int `json:"Fo"` + Op map[string]int `json:"op,omitempty"` + Tilde map[string]int `json:"t~1"` + decodeNamed `json:"named"` + decodeLevel //nolint:unused // encoding/json skips it + decodelevel struct{ Q int } //nolint:unused // encoding/json skips it + Level map[string]int `json:"DecodeLevel"` + Lf map[string]int `json:"LF"` + Backslash map[string]int `json:"bs\\x"` //nolint:staticcheck // the name "bs" + Nine map[string]int `json:"9\\x"` //nolint:staticcheck // no name + Quote int `json:"'q'"` + QuoteK map[string]int `json:"Quote"` + Utf0 int `json:"U\ufffdV"` + Utf map[string]int `json:"u\xffv"` //nolint:staticcheck // the name "u\ufffdv" + Cs int `json:"cs,case:strict"` + Ce map[string]int `json:"ce,case:strict"` + CsK map[string]int `json:"Cs"` + Ci map[string]int `json:"ci,case:strict,case:ignore"` //nolint:staticcheck // not strict + Inline struct { + In map[string]int `json:"in"` + } `json:",embed"` + Rest struct { + K int `json:"k"` + More map[string]int `json:",embed"` // not the deeper Less + DecodeDeeper + } `json:"rest"` + Raw jsontext.Value `json:",embed"` + RawK map[string]int `json:"raw"` + KM map[decodeKey]int `json:",embed"` // plain fields: no string keys + IK map[int]int `json:",embed"` // or keys that unmarshal +} + +type decodeKey string + +func (k *decodeKey) UnmarshalText(b []byte) error { *k = decodeKey(b); return nil } + +type decodeEmbedded struct { + E int `json:"e"` + X int + Dp int `json:"dp"` + Tg map[string]int `json:"Tg"` + DecodeLeaf +} + +type DecodePtrEmbedded struct { + P int `json:"p"` + X int + Tg int + DecodeLeaf +} + +// DecodeLeaf is embedded twice at one depth, so encoding/json drops lf. +type DecodeLeaf struct { + Lf int `json:"lf"` + *DecodeLeaf +} + +type DecodeDeeper struct { + Less map[string]bool `json:",embed"` +} + +type decodeNamed struct { + K int `json:"k"` +} + +type decodeLevel int //nolint:unused // embedded in decodePathReq + +// decodeCount counts the decodes of the body that holds it. +type decodeCount int + +func (c *decodeCount) UnmarshalJSON([]byte) error { *c++; return nil } + +// refusesNull takes a JSON string only. Like a hand-written UnmarshalJSON +// can, it refuses null too, with a type error that names its member. +type refusesNull struct{} + +func (*refusesNull) UnmarshalJSON(b []byte) error { + if b[0] == '"' { + return nil + } + return &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[string](), Field: "strict"} +} + +// TestUnmarshalBodyPaths: unmarshalBody names each wrong-type field +// whatever its member names hold, and decodes the body once for each error +// of encoding/json and once more. A struct member is the field that +// encoding/json decodes it into, a map member its exact key; of two values +// with one path, the wrong one is the one of the type that the error names. +// When null does not fix a field (a map key that does not decode, an +// UnmarshalJSON that refuses null), it names that field, with the error of +// encoding/json as for any lone field, and stops: at once when the field is +// null already. +func TestUnmarshalBodyPaths(t *testing.T) { + for _, tc := range []struct { + name, body string + want []string + decodes int + }{ + {"a key with a dot", `{"m":{"a.b":"x","c":"y"},"count":"z"}`, []string{"m.a.b", "m.c", "count"}, 4}, + {"a key with a slash", `{"m":{"a/b":"x","c":"y"}}`, []string{"m.a~1b", "m.c"}, 3}, + {"a key with a tilde", `{"m":{"a~b":"x","c":"y"}}`, []string{"m.a~0b", "m.c"}, 3}, + {"a key that reads as an escaped slash", `{"m":{"a/b":"x","a~1b":"y","c":"z"}}`, []string{"m.a~1b", "m.a~01b", "m.c"}, 4}, + {"nested maps", `{"nested":{"a.b":{"c/d":"x"},"e":{"f":"y"}},"count":"z"}`, []string{"nested.a.b.c~1d", "nested.e.f", "count"}, 4}, + {"two values with one path", `{"nested":{"a.b":{"c":"x"},"a":{"b.c":"y"}},"count":"z"}`, []string{"nested.a.b.c", "count"}, 3}, + {"a map key that does not decode", `{"keys":{"x":1}}`, []string{"keys.x"}, 2}, + {"a map key that does not decode, null already", `{"keys":{"x":null}}`, []string{"keys.x"}, 1}, + {"an UnmarshalJSON that refuses null", `{"strict":5}`, []string{"strict"}, 2}, + {"a longer key than the path", `{"m":{"a":"x","a.b":1}}`, []string{"m.a"}, 2}, + {"a map key with the name of a field", `{"count":"z","m":{"count":"y"}}`, []string{"count", "m.count"}, 3}, + {"map keys that differ in case", `{"m":{"A":"x","a":"y"}}`, []string{"m.A", "m.a"}, 3}, + {"a key with a dot in another case", `{"nested":{"A":{"b":{}},"a.b":7}}`, []string{"nested.A.b", "nested.a.b"}, 3}, + {"a value with the path of a wrong one", `{"nested":{"a":{"b":"x"},"a.b":{"x":"y"}}}`, []string{"nested.a.b", "nested.a.b.x"}, 3}, + {"two wrong values with one path", `{"nested":{"a":{"b":"x"},"a.b":5},"count":"z"}`, []string{"nested.a.b", "count"}, 4}, + {"fields of embedded structs", `{"e":"x","p":"y","count":"z"}`, []string{"e", "p", "count"}, 4}, + {"a field that two embedded structs hide", `{"X":"a","x":"b"}`, []string{"X"}, 2}, + {"a field name with a dot and a slash", `{"a.b/c":"x","A.B/C":"y","count":"z"}`, []string{"a.b~1c", "count"}, 3}, + {"the shallower or only tagged field of a name, a dash", `{"dp":{"k":"x"},"Tg":{"k":"y"},"-":{"k":"w"},"count":"z"}`, []string{"dp.k", "Tg.k", "-.k", "count"}, 5}, + {"the exact name, then the first in another case", `{"ex":{"k":"x"},"FO":{"k":"y"},"count":"z"}`, []string{"ex.k", "FO.k", "count"}, 4}, + {"options, a named embedded struct, a name with ~1", `{"op":{"k":"x"},"named":{"k":"y"},"t~1":{"k":"w"},"count":"z"}`, []string{"op.k", "named.k", "t~01.k", "count"}, 5}, + {"an embedded int that is not exported, a dropped name", `{"DECODELEVEL":{"k":"x"},"lf":{"k":"y"},"count":"z"}`, []string{"DECODELEVEL.k", "lf.k", "count"}, 4}, + {"a tag name with a backslash, a quote, bad UTF-8", `{"bs":{"k":"x"},"Nine":{"k":"v"},"Quote":{"k":"y"},"u\ufffdv":{"k":"w"},"count":"z"}`, []string{"bs.k", "Nine.k", "Quote.k", "u\ufffdv.k", "count"}, 6}, + {"case:strict", `{"CS":{"k":"x"},"ce":{"k":"v"},"CI":{"k":"y"},"count":"z"}`, []string{"CS.k", "ce.k", "CI.k", "count"}, 5}, + {",embed", `{"in":{"k":"x"},"rest":{"zz":"y","k":"w"},"count":"z"}`, []string{"in.k", "rest.zz", "rest.k", "count"}, 5}, + {",embed jsontext.Value, maps without string keys", `{"Raw":{"k":"x"},"KM":{"k":"y"},"IK":{"7":"w"},"count":"z"}`, []string{"Raw.k", "KM.k", "IK.7", "count"}, 5}, + } { + t.Run(tc.name, func(t *testing.T) { + var dst decodePathReq + err := unmarshalBody([]byte(`{"n":0,`+tc.body[1:]), &dst) // n counts the decodes + var got []string + for _, p := range invalidParamsFor(err) { + got = append(got, p.Name) + } + _, lone := errors.AsType[*json.UnmarshalTypeError](err) + if !slices.Equal(got, tc.want) || int(dst.Decodes) != tc.decodes || lone != (len(tc.want) == 1) { + t.Fatalf("named %q in %d decodes (error %T), want %q in %d", got, dst.Decodes, err, tc.want, tc.decodes) + } + }) + } +}