encoding/cbor: fix order-dependent partial unmarshals

This commit is contained in:
andzdroid
2026-03-15 12:48:41 +00:00
parent 117e3a7b5a
commit f123fb1e91
2 changed files with 3 additions and 12 deletions

View File

@@ -661,8 +661,7 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header,
unknown := length == -1
fields := reflect.struct_fields_zipped(ti.id)
idx := 0
for ; idx < len(fields) && (unknown || idx < length); idx += 1 {
for idx := 0; unknown || idx < length; idx += 1 {
// Decode key, keys can only be strings.
key: string
if keyv, kerr := decode_key(d, v, context.temp_allocator); unknown && kerr == .Break {
@@ -710,16 +709,6 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header,
_unmarshal_value(d, fany, _decode_header(r) or_return) or_return
}
// If there are fields left in the map that did not get decoded into the struct, decode and discard them.
if !unknown {
for _ in idx..<length {
key := err_conv(_decode_from_decoder(d, allocator=context.temp_allocator)) or_return
destroy(key, context.temp_allocator)
val := err_conv(_decode_from_decoder(d, allocator=context.temp_allocator)) or_return
destroy(val, context.temp_allocator)
}
}
return
case reflect.Type_Info_Map:

View File

@@ -427,6 +427,7 @@ test_unmarshal_map_into_struct_partially :: proc(t: ^testing.T) {
}
Foo_More :: struct {
foo: bool,
bar: struct {
hello: string,
world: string,
@@ -436,6 +437,7 @@ test_unmarshal_map_into_struct_partially :: proc(t: ^testing.T) {
baz: int,
}
more := Foo_More{
foo = false,
bar = {
hello = "hello",
world = "world",