diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2013-07-22 18:59:15 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2013-07-22 18:59:15 +0300 |
commit | 1f13e8cd2c5bb091677f35e53ae5757774e7d1ba (patch) | |
tree | 99bfe3fcafe147f514f00128615a0a7b850d53ea | |
parent | 64947cb382271e2ab17dbf40ab634846d7d30ad9 (diff) |
Fix bugs in extension support when multiple extension fields are present.
-rw-r--r-- | generator/nanopb_generator.py | 9 | ||||
-rw-r--r-- | pb.h | 11 | ||||
-rw-r--r-- | pb_encode.c | 7 |
3 files changed, 23 insertions, 4 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 3bac9a92..f39614b3 100644 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -313,16 +313,19 @@ class ExtensionField(Field): raise NotImplementedError("Only 'optional' is supported for extension fields. " + "(%s.rules == %s)" % (self.fullname, self.rules)) + self.rules = 'OPTEXT' + def extension_decl(self): '''Declaration of the extension type in the .pb.h file''' - return 'extern const pb_extension_type_t %s;' % self.fullname + return 'extern const pb_extension_type_t %s;\n' % self.fullname def extension_def(self): '''Definition of the extension type in the .pb.c file''' + result = 'typedef struct {\n' result += str(self) - result += '} %s;\n' % self.struct_name - result += ('static const pb_field_t %s_field = %s;\n' % + result += '\n} %s;\n\n' % self.struct_name + result += ('static const pb_field_t %s_field = \n %s;\n\n' % (self.fullname, self.pb_field_t(None))) result += 'const pb_extension_type_t %s = {\n' % self.fullname result += ' NULL,\n' @@ -364,6 +364,17 @@ struct _pb_extension_t { {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \ pb_delta_end(st, m, pm), 0, pb_membersize(st, m), 0, ptr} +/* Optional extensions don't have the has_ field, as that would be redundant. */ +#define PB_OPTEXT_STATIC(tag, st, m, pm, ltype, ptr) \ + {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \ + 0, \ + 0, \ + pb_membersize(st, m), 0, ptr} + +#define PB_OPTEXT_CALLBACK(tag, st, m, pm, ltype, ptr) \ + {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \ + 0, 0, pb_membersize(st, m), 0, ptr} + /* The mapping from protobuf types to LTYPEs is done using these macros. */ #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES diff --git a/pb_encode.c b/pb_encode.c index 58d76a7a..d6ba7e34 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -161,9 +161,14 @@ static bool checkreturn encode_static_field(pb_ostream_t *stream, { pb_encoder_t func; const void *pSize; + bool dummy = true; func = PB_ENCODERS[PB_LTYPE(field->type)]; - pSize = (const char*)pData + field->size_offset; + + if (field->size_offset) + pSize = (const char*)pData + field->size_offset; + else + pSize = &dummy; switch (PB_HTYPE(field->type)) { |