From ba93474aa223bdaf7cb524c002824096a4837146 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Wed, 7 Mar 2018 21:07:14 +0000 Subject: Fix oneof encoding/decoding --- upb/encode.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'upb/encode.c') diff --git a/upb/encode.c b/upb/encode.c index f01ad0c..67678fe 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -360,7 +360,13 @@ bool upb_encode_message(upb_encstate* e, const char *msg, CHK(upb_encode_array(e, msg + f->offset, m, f)); } else { if (upb_encode_hasscalarfield(msg, m, f)) { - CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); + if (f->oneof_index != UPB_NOT_IN_ONEOF) { + const upb_msglayout_oneofinit_v1 *o = &m->oneofs[f->oneof_index]; + CHK(upb_encode_scalarfield(e, msg + o->data_offset, + m, f, !m->is_proto2)); + } else { + CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); + } } } } -- cgit v1.2.3 From dfe7d3a3749ff1cdb55d839e3dbfda46b8b2ee72 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Wed, 7 Mar 2018 21:13:32 +0000 Subject: swap condition branches to remove negation --- upb/encode.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'upb/encode.c') diff --git a/upb/encode.c b/upb/encode.c index 67678fe..abcf17c 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -360,12 +360,12 @@ bool upb_encode_message(upb_encstate* e, const char *msg, CHK(upb_encode_array(e, msg + f->offset, m, f)); } else { if (upb_encode_hasscalarfield(msg, m, f)) { - if (f->oneof_index != UPB_NOT_IN_ONEOF) { + if (f->oneof_index == UPB_NOT_IN_ONEOF) { + CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); + } else { const upb_msglayout_oneofinit_v1 *o = &m->oneofs[f->oneof_index]; CHK(upb_encode_scalarfield(e, msg + o->data_offset, m, f, !m->is_proto2)); - } else { - CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); } } } -- cgit v1.2.3