upb: fix the map encoder to stop dying under ubsan

PiperOrigin-RevId: 601004095
pull/15558/head
Eric Salo 10 months ago committed by Copybara-Service
parent bf00034493
commit 9d625a1df7
  1. 1
      upb/message/map_sorter.c
  2. 2
      upb/text/encode.c
  3. 2
      upb/wire/encode.c

@ -113,6 +113,7 @@ static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted,
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted) { const upb_Map* map, _upb_sortedmap* sorted) {
int map_size = _upb_Map_Size(map); int map_size = _upb_Map_Size(map);
UPB_ASSERT(map_size);
if (!_upb_mapsorter_resize(s, sorted, map_size)) return false; if (!_upb_mapsorter_resize(s, sorted, map_size)) return false;

@ -357,6 +357,8 @@ static void txtenc_map(txtenc* e, const upb_Map* map, const upb_FieldDef* f) {
txtenc_mapentry(e, key, val, f); txtenc_mapentry(e, key, val, f);
} }
} else { } else {
if (upb_Map_Size(map) == 0) return;
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f); const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key_f = upb_MessageDef_Field(entry, 0); const upb_FieldDef* key_f = upb_MessageDef_Field(entry, 0);
_upb_sortedmap sorted; _upb_sortedmap sorted;

@ -439,7 +439,7 @@ static void encode_map(upb_encstate* e, const upb_Message* msg,
upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]); upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(layout->UPB_PRIVATE(field_count) == 2); UPB_ASSERT(layout->UPB_PRIVATE(field_count) == 2);
if (map == NULL) return; if (!map || !upb_Map_Size(map)) return;
if (e->options & kUpb_EncodeOption_Deterministic) { if (e->options & kUpb_EncodeOption_Deterministic) {
_upb_sortedmap sorted; _upb_sortedmap sorted;

Loading…
Cancel
Save