diff options
author | Petteri Aimonen <jpa@github.mail.kapsi.fi> | 2020-03-26 13:57:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-26 13:57:03 +0200 |
commit | 0bf6bef725a7519150fd98f532f14c88845bc09f (patch) | |
tree | 2765e3cdf44f13ba31da2617957227b372c33d4e | |
parent | accfbbbd6840dd796efe835a0bf4f89a0835c238 (diff) | |
parent | b233555f55628d0097266ea7ae653f5ec52f3732 (diff) | |
download | nanopb-c-0bf6bef725a7519150fd98f532f14c88845bc09f.tar.gz |
Merge pull request #504 from LedgerHQ/maintenance_0.3
Regression test + fix for proto3 submessage improperly considered empty
-rw-r--r-- | pb_encode.c | 6 | ||||
-rw-r--r-- | tests/regression/issue_504/SConscript | 12 | ||||
-rw-r--r-- | tests/regression/issue_504/test.c | 47 | ||||
-rw-r--r-- | tests/regression/issue_504/test.proto | 13 |
4 files changed, 78 insertions, 0 deletions
diff --git a/pb_encode.c b/pb_encode.c index 0f89d98..be82510 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -304,6 +304,12 @@ static bool pb_check_proto3_default_value(const pb_field_t *field, const void *p return true; } } + + /* Compares pointers to NULL in case of FT_POINTER */ + if (PB_ATYPE(type) == PB_ATYPE_POINTER && PB_LTYPE(type) > PB_LTYPE_LAST_PACKABLE) + { + return !*(const void**)((uintptr_t)pData); + } { /* Catch-all branch that does byte-per-byte comparison for zero value. diff --git a/tests/regression/issue_504/SConscript b/tests/regression/issue_504/SConscript new file mode 100644 index 0000000..c303171 --- /dev/null +++ b/tests/regression/issue_504/SConscript @@ -0,0 +1,12 @@ +# Regression test for #504: +# Non empty submessage considered empty on FT_POINTER fields with address aligned on 0x100 + +Import('env', 'malloc_env') + +env.NanopbProto(["test.proto"]) +test = malloc_env.Program(["test.c", + "test.pb.c", + "$COMMON/pb_encode.o", + "$COMMON/pb_common.o"]) + +env.RunTest(test) diff --git a/tests/regression/issue_504/test.c b/tests/regression/issue_504/test.c new file mode 100644 index 0000000..1ca86db --- /dev/null +++ b/tests/regression/issue_504/test.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <pb_encode.h> +#include "test.pb.h" +#include "unittests.h" + +const char STR[] = "test str"; +#define ALIGN 0x100 + +int main(int argc, char **argv) +{ + int status = 0; + uint8_t buffer[512] = {0}; + int i; + pb_ostream_t ostream; + MyMessage msg = MyMessage_init_zero; + char *pStr, *pStrAligned; + ostream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + + /* copy STR to a malloced 0x100 aligned address */ + pStr = malloc(sizeof(STR) + ALIGN); + pStrAligned = (char*)((uintptr_t)(pStr + ALIGN) & ~(ALIGN - 1)); + memcpy(pStrAligned, STR, sizeof(STR)); + + msg.submessage.somestring = pStrAligned; + printf("%p: '%s'\n", msg.submessage.somestring, msg.submessage.somestring); + + if (!pb_encode(&ostream, MyMessage_fields, &msg)) { + fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&ostream)); + return 1; + } + + free(pStr); + msg.submessage.somestring = NULL; + + printf("response payload (%d):", (int)ostream.bytes_written); + for (i = 0; i < ostream.bytes_written; i++) { + printf("%02X", buffer[i]); + } + printf("\n"); + + TEST(ostream.bytes_written != 0); + + return status; +} + diff --git a/tests/regression/issue_504/test.proto b/tests/regression/issue_504/test.proto new file mode 100644 index 0000000..a5b0d9c --- /dev/null +++ b/tests/regression/issue_504/test.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +import "nanopb.proto"; + +message MyMessage +{ + SubMessage submessage = 1; +} + +message SubMessage +{ + string somestring = 1 [(nanopb).type = FT_POINTER]; +} |