Skip to content

Commit

Permalink
Fix upb_MiniTableField_CType() to work correctly for enums & strings.
Browse files Browse the repository at this point in the history
`upb_MiniTableField_CType()` was directly checking the `descriptortype` in `upb_MiniTableField`, without taking `field->mode` into account -- which can indicate that an int32 is actually an enum, or that bytes is really a string.

PiperOrigin-RevId: 537975302
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jun 5, 2023
1 parent 3c76a26 commit 532b4d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
13 changes: 0 additions & 13 deletions upb/mini_table/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,6 @@ const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
return NULL;
}

upb_FieldType upb_MiniTableField_Type(const upb_MiniTableField* field) {
if (field->mode & kUpb_LabelFlags_IsAlternate) {
if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Int32) {
return kUpb_FieldType_Enum;
} else if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes) {
return kUpb_FieldType_String;
} else {
UPB_ASSERT(false);
}
}
return field->UPB_PRIVATE(descriptortype);
}

static bool upb_MiniTable_Is_Oneof(const upb_MiniTableField* f) {
return f->presence < 0;
}
Expand Down
16 changes: 14 additions & 2 deletions upb/mini_table/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,22 @@ UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_GetFieldByIndex(
return &t->fields[index];
}

UPB_API upb_FieldType upb_MiniTableField_Type(const upb_MiniTableField* field);
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const upb_MiniTableField* field) {
if (field->mode & kUpb_LabelFlags_IsAlternate) {
if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Int32) {
return kUpb_FieldType_Enum;
} else if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes) {
return kUpb_FieldType_String;
} else {
UPB_ASSERT(false);
}
}
return (upb_FieldType)field->UPB_PRIVATE(descriptortype);
}

UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f) {
switch (f->UPB_PRIVATE(descriptortype)) {
switch (upb_MiniTableField_Type(f)) {
case kUpb_FieldType_Double:
return kUpb_CType_Double;
case kUpb_FieldType_Float:
Expand Down
1 change: 1 addition & 0 deletions upb/reflection/message_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
UPB_ASSERT(layout_index < m->layout->field_count);
const upb_MiniTableField* mt_f = &m->layout->fields[layout_index];
UPB_ASSERT(upb_FieldDef_Type(f) == upb_MiniTableField_Type(mt_f));
UPB_ASSERT(upb_FieldDef_CType(f) == upb_MiniTableField_CType(mt_f));
UPB_ASSERT(upb_FieldDef_HasPresence(f) ==
upb_MiniTableField_HasPresence(mt_f));
}
Expand Down

0 comments on commit 532b4d0

Please sign in to comment.