diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2016-12-31 10:33:48 +0200 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2016-12-31 10:42:22 +0200 |
commit | 48f5dd83530a46dd2423277f4488fd4ce9c93b72 (patch) | |
tree | 366642f135defe7d57fe9b88a705d5fe3cce24f4 /pb.h | |
parent | 90a19bf0368035147a74b9b8edd65d672a03cd00 (diff) |
Fix multiple oneofs in same message (issue #229)
Previously the field iterator logic didn't know whether two
oneof fields were part of the same union, or separate. This
caused wrong pointers to be calculated if multiple oneofs were
inside a single message.
This commit fixes this by using dataoffset of PB_SIZE_MAX to
indicate union fields after the first field.
Theoretically PB_SIZE_MAX is also a valid value for data offset,
which could cause errors. Adding a compile-time assert for this
is somewhat difficult. However I consider it extremely unlikely
that there is any platform that could trigger this situation, as
it would require 255 bytes of extra data/padding between two protobuf
oneof fields. On 64-bit architectures the worst case is 16 bytes,
and even esoteric platforms only align to 64 bytes or so. Manual
modification of the generated .pb.h file could trigger this, but
even then it would require pretty bad luck to happen.
Diffstat (limited to 'pb.h')
-rw-r--r-- | pb.h | 2 |
1 files changed, 2 insertions, 0 deletions
@@ -393,6 +393,8 @@ struct pb_extension_s { #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1)) /* data_offset for subsequent fields */ #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2)) +/* data offset for subsequent fields inside an union (oneof) */ +#define PB_DATAOFFSET_UNION(st, m1, m2) (PB_SIZE_MAX) /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */ #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \ ? PB_DATAOFFSET_FIRST(st, m1, m2) \ |