Fix missing warning with large bytes fields (issue #220)
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 23 Oct 2016 11:01:10 +0000 (14:01 +0300)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 23 Oct 2016 11:02:06 +0000 (14:02 +0300)
Need to generate compile time check if the bytes field + size field
might exceed 255 bytes. Also eliminated spurious checks generated
for some callback fields.

generator/nanopb_generator.py

index 185a97b..5c53a63 100755 (executable)
@@ -530,8 +530,8 @@ class Field:
         '''Determine if this field needs 16bit or 32bit pb_field_t structure to compile properly.
         Returns numeric value or a C-expression for assert.'''
         check = []
-        if self.pbtype == 'MESSAGE':
-            if self.rules == 'REPEATED' and self.allocation == 'STATIC':
+        if self.pbtype == 'MESSAGE' and self.allocation == 'STATIC':
+            if self.rules == 'REPEATED':
                 check.append('pb_membersize(%s, %s[0])' % (self.struct_name, self.name))
             elif self.rules == 'ONEOF':
                 if self.anonymous:
@@ -540,6 +540,9 @@ class Field:
                     check.append('pb_membersize(%s, %s.%s)' % (self.struct_name, self.union_name, self.name))
             else:
                 check.append('pb_membersize(%s, %s)' % (self.struct_name, self.name))
+        elif self.pbtype == 'BYTES' and self.allocation == 'STATIC':
+            if self.max_size > 251:
+                check.append('pb_membersize(%s, %s)' % (self.struct_name, self.name))
 
         return FieldMaxSize([self.tag, self.max_size, self.max_count],
                             check,