diff options
-rw-r--r-- | pb_decode.c | 9 | ||||
-rw-r--r-- | pb_encode.c | 28 | ||||
-rw-r--r-- | tests/Makefile | 10 |
3 files changed, 30 insertions, 17 deletions
diff --git a/pb_decode.c b/pb_decode.c index 601dc742..2235280e 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -612,6 +612,7 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field { bool status; pb_istream_t substream; + const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr; if (!pb_make_string_substream(stream, &substream)) return false; @@ -619,7 +620,13 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field if (field->ptr == NULL) PB_RETURN_ERROR(stream, "invalid field descriptor"); - status = pb_decode_noinit(&substream, (pb_field_t*)field->ptr, dest); + /* New array entries need to be initialized, while required and optional + * submessages have already been initialized in the top-level pb_decode. */ + if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY) + status = pb_decode(&substream, submsg_fields, dest); + else + status = pb_decode_noinit(&substream, submsg_fields, dest); + pb_close_string_substream(stream, &substream); return status; } diff --git a/pb_encode.c b/pb_encode.c index 3fc03811..9a6ad99f 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -168,7 +168,7 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons break; case PB_HTYPE_OPTIONAL: - if (*(bool*)pSize) + if (*(const bool*)pSize) { if (!pb_encode_tag_for_field(stream, field)) return false; @@ -179,13 +179,13 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons break; case PB_HTYPE_ARRAY: - if (!encode_array(stream, field, pData, *(size_t*)pSize, func)) + if (!encode_array(stream, field, pData, *(const size_t*)pSize, func)) return false; break; case PB_HTYPE_CALLBACK: { - pb_callback_t *callback = (pb_callback_t*)pData; + const pb_callback_t *callback = (const pb_callback_t*)pData; if (callback->funcs.encode != NULL) { if (!callback->funcs.encode(stream, field, callback->arg)) @@ -243,7 +243,7 @@ bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value) lebytes[3] = bytes[0]; return pb_write(stream, lebytes, 4); #else - return pb_write(stream, (uint8_t*)value, 4); + return pb_write(stream, (const uint8_t*)value, 4); #endif } @@ -262,7 +262,7 @@ bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value) lebytes[7] = bytes[0]; return pb_write(stream, lebytes, 8); #else - return pb_write(stream, (uint8_t*)value, 8); + return pb_write(stream, (const uint8_t*)value, 8); #endif } @@ -358,10 +358,10 @@ bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, co switch (field->data_size) { - case 1: value = *(uint8_t*)src; break; - case 2: value = *(uint16_t*)src; break; - case 4: value = *(uint32_t*)src; break; - case 8: value = *(uint64_t*)src; break; + case 1: value = *(const uint8_t*)src; break; + case 2: value = *(const uint16_t*)src; break; + case 4: value = *(const uint32_t*)src; break; + case 8: value = *(const uint64_t*)src; break; default: return false; } @@ -374,8 +374,8 @@ bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, c switch (field->data_size) { - case 4: value = *(int32_t*)src; break; - case 8: value = *(int64_t*)src; break; + case 4: value = *(const int32_t*)src; break; + case 8: value = *(const int64_t*)src; break; default: return false; } @@ -396,7 +396,7 @@ bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, c bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src) { - pb_bytes_array_t *bytes = (pb_bytes_array_t*)src; + const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src; UNUSED(field); return pb_encode_string(stream, bytes->bytes, bytes->size); } @@ -404,7 +404,7 @@ bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, con bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src) { UNUSED(field); - return pb_encode_string(stream, (uint8_t*)src, strlen((char*)src)); + return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src)); } bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src) @@ -412,6 +412,6 @@ bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field if (field->ptr == NULL) return false; - return pb_encode_submessage(stream, (pb_field_t*)field->ptr, src); + return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src); } diff --git a/tests/Makefile b/tests/Makefile index 5014221e..3810c95f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -4,7 +4,7 @@ DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests. TESTS=test_decode1 test_encode1 decode_unittests encode_unittests test_no_messages # More strict checks for the core part of nanopb -CFLAGS_CORE=-pedantic -Wextra +CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wlogical-op all: breakpoints $(TESTS) run_unittests @@ -20,6 +20,12 @@ pb_encode.o: ../pb_encode.c $(DEPS) pb_decode.o: ../pb_decode.c $(DEPS) $(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $< +pb_encode.cxx.o: ../pb_encode.c $(DEPS) + $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $< +pb_decode.cxx.o: ../pb_decode.c $(DEPS) + $(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $< + +test_cxxcompile: pb_encode.cxx.o pb_decode.cxx.o test_decode1: test_decode1.o pb_decode.o person.pb.o test_decode2: test_decode2.o pb_decode.o person.pb.o test_decode3: test_decode3.o pb_decode.o alltypes.pb.o @@ -46,7 +52,7 @@ coverage: run_unittests gcov pb_encode.gcda gcov pb_decode.gcda -run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields +run_unittests: decode_unittests encode_unittests test_cxxcompile test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields rm -f *.gcda ./decode_unittests > /dev/null |