bug: Unmarshal panics instead of returning an error for non-map[string]any-shaped map targets #15
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
In
encoding/systemd/marshal_decode.go,decodeFileToMapReflect(~lines 143–152), reached fromdecodeFileNode'sreflect.Mapbranch:decodeFileToMapalways builds an intermediatemap[string]any(sections) ofmap[string]any(keys, with[]stringfor duplicate keys).decodeFileToMapReflectthen does:for every entry without checking that
v's dynamic type is assignable to the target's declared map value type. Any target map type other than exactlymap[string]any/any(e.g. the very naturalmap[string]string,map[string]int, or evenmap[string]map[string]string), or any target whose section has a duplicate key producing a[]stringwhere a scalar was expected, causesreflect.Value.SetMapIndexto panic rather thanUnmarshalreturning anerroras its signature promises.Reproduction
All three of these panic instead of returning an error:
Recommended fix
Before
SetMapIndex, check assignability (reflect.TypeOf(v).AssignableTo(target.Type().Elem())) and either convert when possible or return a descriptivefmt.Errorf(mirroring the existing"systemd: map key must be string, got %s"style error already used elsewhere in this file) instead of letting the panic propagate. Consider supportingmap[string]string/map[string]map[string]stringdirectly since they're at least as natural a target asmap[string]any.