diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2024-10-25 20:37:55 +0200 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2024-10-25 20:37:55 +0200 |
| commit | 793ef6012b5a7844cee392d51999cb566af4a787 (patch) | |
| tree | 8193053aeb17e2e16e23e32a795aa5278020aa19 /core/encoding | |
| parent | bcf2b93c6e7bf6e4d94685f2c841d6243bceb4c3 (diff) | |
encoding/cbor: handle binary having more fields than the struct by discarding
Diffstat (limited to 'core/encoding')
| -rw-r--r-- | core/encoding/cbor/unmarshal.odin | 14 |
1 files changed, 13 insertions, 1 deletions
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..<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: |