diff --git a/upb/def.c b/upb/def.c index 04b41cee7c..5394955b12 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1027,7 +1027,8 @@ static void assign_layout_indices(const upb_msgdef *m, upb_msglayout *l, upb_fielddef *f = (upb_fielddef*)upb_msgdef_itof(m, fields[i].number); UPB_ASSERT(f); f->layout_index = i; - if (i < UINT8_MAX && upb_fielddef_number(f) == i + 1) { + if (i < UINT8_MAX && fields[i].number == i + 1 && + (i == 0 || fields[i-1].number == i)) { dense_below = i + 1; } } diff --git a/upbc/protoc-gen-upb.cc b/upbc/protoc-gen-upb.cc index 863bd2eb5a..159a8ecc45 100644 --- a/upbc/protoc-gen-upb.cc +++ b/upbc/protoc-gen-upb.cc @@ -868,6 +868,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output, std::string fields_array_ref = "NULL"; std::string submsgs_array_ref = "NULL"; uint8_t dense_below = 0; + int dense_below_max = std::numeric_limits::max(); MessageLayout layout(message); SubmsgArray submsg_array(message); @@ -893,14 +894,14 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output, fields_array_ref = "&" + fields_array_name + "[0]"; output("static const upb_msglayout_field $0[$1] = {\n", fields_array_name, field_number_order.size()); - for (auto field : field_number_order) { + for (int i = 0; i < static_cast(field_number_order.size()); i++) { + auto field = field_number_order[i]; int submsg_index = 0; std::string presence = "0"; - if (field->number() <= - std::numeric_limits::max() && - dense_below + 1 == field->number()) { - dense_below = field->number(); + if (i < dense_below_max && field->number() == i + 1 && + (i == 0 || field_number_order[i - 1]->number() == i)) { + dense_below = i + 1; } if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {