aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2015-10-05 16:01:53 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2015-10-05 16:01:53 +0300
commit7f1e0ec17987ff10c5ce17f347b66423fe8ac4bd (patch)
tree968ebd9aa8ffcae0d45929a1c9bdc5abd2d02c86
parent6448f5d40d498a8de05b953f8318dba0cf4ff26f (diff)
Fix regression in generating message size defines (issue #172).
This bug was triggered when: 1. A .proto file included another .proto from a different directory. 2. The another .proto has an associated .options file. Added regression test for the same.
-rwxr-xr-xgenerator/nanopb_generator.py18
-rw-r--r--tests/regression/issue_172/SConscript16
-rw-r--r--tests/regression/issue_172/msg_size.c9
-rw-r--r--tests/regression/issue_172/submessage/submessage.options1
-rw-r--r--tests/regression/issue_172/submessage/submessage.proto4
-rw-r--r--tests/regression/issue_172/test.proto6
6 files changed, 45 insertions, 9 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 37f7beb5..bc388958 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -514,18 +514,18 @@ class Field:
return None
if self.pbtype == 'MESSAGE':
+ encsize = None
if str(self.submsgname) in dependencies:
submsg = dependencies[str(self.submsgname)]
encsize = submsg.encoded_size(dependencies)
- if encsize is None:
- return None # Submessage size is indeterminate
-
- # Include submessage length prefix
- encsize += varint_max_size(encsize.upperlimit())
- else:
- # Submessage cannot be found, this currently occurs when
- # the submessage type is defined in a different file and
- # not using the protoc plugin.
+ if encsize is not None:
+ # Include submessage length prefix
+ encsize += varint_max_size(encsize.upperlimit())
+
+ if encsize is None:
+ # Submessage or its size cannot be found.
+ # This can occur if submessage is defined in different
+ # file, and it or its .options could not be found.
# Instead of direct numeric value, reference the size that
# has been #defined in the other file.
encsize = EncodedSize(self.submsgname + 'size')
diff --git a/tests/regression/issue_172/SConscript b/tests/regression/issue_172/SConscript
new file mode 100644
index 00000000..49c919e8
--- /dev/null
+++ b/tests/regression/issue_172/SConscript
@@ -0,0 +1,16 @@
+# Verify that _size define is generated for messages that have
+# includes from another directory.
+
+Import('env')
+
+incpath = env.Clone()
+incpath.Append(PROTOCPATH="#regression/issue_172/submessage")
+incpath.Append(CPPPATH="$BUILD/regression/issue_172/submessage")
+incpath.NanopbProto('test')
+incpath.NanopbProto(['submessage/submessage', 'submessage/submessage.options'])
+
+p = incpath.Program(["msg_size.c",
+ "test.pb.c",
+ "submessage/submessage.pb.c"])
+
+
diff --git a/tests/regression/issue_172/msg_size.c b/tests/regression/issue_172/msg_size.c
new file mode 100644
index 00000000..be45acb4
--- /dev/null
+++ b/tests/regression/issue_172/msg_size.c
@@ -0,0 +1,9 @@
+#include "test.pb.h"
+
+PB_STATIC_ASSERT(testmessage_size >= 1+1+1+1+16, TESTMESSAGE_SIZE_IS_WRONG)
+
+int main()
+{
+ return 0;
+}
+
diff --git a/tests/regression/issue_172/submessage/submessage.options b/tests/regression/issue_172/submessage/submessage.options
new file mode 100644
index 00000000..12fb1984
--- /dev/null
+++ b/tests/regression/issue_172/submessage/submessage.options
@@ -0,0 +1 @@
+submessage.data max_size: 16
diff --git a/tests/regression/issue_172/submessage/submessage.proto b/tests/regression/issue_172/submessage/submessage.proto
new file mode 100644
index 00000000..ce6804af
--- /dev/null
+++ b/tests/regression/issue_172/submessage/submessage.proto
@@ -0,0 +1,4 @@
+syntax = "proto2";
+message submessage {
+ required bytes data = 1;
+}
diff --git a/tests/regression/issue_172/test.proto b/tests/regression/issue_172/test.proto
new file mode 100644
index 00000000..fbd97be5
--- /dev/null
+++ b/tests/regression/issue_172/test.proto
@@ -0,0 +1,6 @@
+syntax = "proto2";
+import "submessage.proto";
+
+message testmessage {
+ optional submessage sub = 1;
+}