encoding/cbor: handle binary having more fields than the struct by discarding

This commit is contained in:
Laytan Laats
2024-10-25 20:37:55 +02:00
parent bcf2b93c6e
commit 793ef6012b
2 changed files with 64 additions and 1 deletions
@@ -382,6 +382,57 @@ test_lying_length_array :: proc(t: ^testing.T) {
testing.expect_value(t, err, io.Error.Unexpected_EOF) // .Out_Of_Memory would be bad.
}
@(test)
test_unmarshal_map_into_struct_partially :: proc(t: ^testing.T) {
// Tests that when unmarshalling into a struct that has less fields than the binary map has fields,
// the additional fields in the binary are skipped and the rest of the binary is correctly decoded.
Foo :: struct {
bar: struct {
hello: string,
world: string,
},
baz: int,
}
Foo_More :: struct {
bar: struct {
hello: string,
world: string,
hellope: string,
},
baz: int,
}
more := Foo_More{
bar = {
hello = "hello",
world = "world",
hellope = "hellope",
},
baz = 4,
}
more_bin, err := cbor.marshal(more)
testing.expect_value(t, err, nil)
less := Foo{
bar = {
hello = "hello",
world = "world",
},
baz = 4,
}
less_out: Foo
uerr := cbor.unmarshal(string(more_bin), &less_out)
testing.expect_value(t, uerr, nil)
testing.expect_value(t, less, less_out)
delete(more_bin)
delete(less_out.bar.hello)
delete(less_out.bar.world)
}
@(test)
test_decode_unsigned :: proc(t: ^testing.T) {
expect_decoding(t, "\x00", "0", u8)