From 793ef6012b5a7844cee392d51999cb566af4a787 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Fri, 25 Oct 2024 20:37:55 +0200 Subject: [PATCH] encoding/cbor: handle binary having more fields than the struct by discarding --- core/encoding/cbor/unmarshal.odin | 14 +++++- tests/core/encoding/cbor/test_core_cbor.odin | 51 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/core/encoding/cbor/unmarshal.odin b/core/encoding/cbor/unmarshal.odin index bccd4db18..ed8e9cbfc 100644 --- a/core/encoding/cbor/unmarshal.odin +++ b/core/encoding/cbor/unmarshal.odin @@ -628,7 +628,8 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, unknown := length == -1 fields := reflect.struct_fields_zipped(ti.id) - for idx := 0; idx < len(fields) && (unknown || idx < length); idx += 1 { + idx := 0 + for ; idx < len(fields) && (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 { @@ -673,6 +674,17 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, fany := any{ptr, field.type.id} _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..