From ef422656a57b3f472b192691a40f48d0d72f2927 Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Sat, 7 Mar 2015 10:25:09 +0200 Subject: [PATCH] Fix oneof submessage initialization bug. Update issue 149 Status: FixedInGit --- pb_decode.c | 4 ++++ tests/oneof/decode_oneof.c | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pb_decode.c b/pb_decode.c index 9d25dc61..4d8c7cbe 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -396,6 +396,10 @@ static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t case PB_HTYPE_ONEOF: *(pb_size_t*)iter->pSize = iter->pos->tag; + if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE) + { + pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData); + } return func(stream, iter->pos, iter->pData); default: diff --git a/tests/oneof/decode_oneof.c b/tests/oneof/decode_oneof.c index 83b4702c..37075cd6 100644 --- a/tests/oneof/decode_oneof.c +++ b/tests/oneof/decode_oneof.c @@ -2,6 +2,7 @@ #include #include +#include #include #include "oneof.pb.h" #include "test_helpers.h" @@ -10,9 +11,12 @@ /* Test the 'OneOfMessage' */ int test_oneof_1(pb_istream_t *stream, int option) { - OneOfMessage msg = OneOfMessage_init_zero; + OneOfMessage msg; int status = 0; + /* To better catch initialization errors */ + memset(&msg, 0xAA, sizeof(msg)); + if (!pb_decode(stream, OneOfMessage_fields, &msg)) { printf("Decoding failed: %s\n", PB_GET_ERROR(stream)); @@ -124,4 +128,4 @@ int main(int argc, char **argv) } return 0; -} \ No newline at end of file +} -- 2.16.6