Fix pb_decode() not initializing fields inside oneof (#635)
This regression was caused by commit edf6dcbffee.
Added test coverage for it now.
diff --git a/pb_decode.c b/pb_decode.c
index 28f6b57..5f3b51e 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -530,8 +530,17 @@
memset(field->pData, 0, (size_t)field->data_size);
/* Set default values for the submessage fields. */
- if (!pb_field_set_to_default(field))
- PB_RETURN_ERROR(stream, "failed to set defaults");
+ if (field->submsg_desc->default_value != NULL ||
+ field->submsg_desc->field_callback != NULL ||
+ field->submsg_desc->submsg_info[0] != NULL)
+ {
+ pb_field_iter_t submsg_iter;
+ if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
+ {
+ if (!pb_message_set_to_defaults(&submsg_iter))
+ PB_RETURN_ERROR(stream, "failed to set defaults");
+ }
+ }
}
*(pb_size_t*)field->pSize = field->tag;
diff --git a/tests/alltypes/decode_alltypes.c b/tests/alltypes/decode_alltypes.c
index e431f35..8e70d86 100644
--- a/tests/alltypes/decode_alltypes.c
+++ b/tests/alltypes/decode_alltypes.c
@@ -213,6 +213,7 @@
TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
TEST(strcmp(alltypes.oneof.oneof_msg1.substuff1, "4059") == 0);
TEST(alltypes.oneof.oneof_msg1.substuff2 == 4059);
+ TEST(alltypes.oneof.oneof_msg1.substuff3 == 3);
TEST(alltypes.has_opt_non_zero_based_enum == true);
TEST(alltypes.opt_non_zero_based_enum == NonZeroBasedEnum_Three);