From 05d23ea95583c74d74783228b47bd0aea99480e4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 20:35:28 -0800 Subject: [PATCH] Fixed UBSAN errors. --- upb/encode.c | 2 +- upb/mini_table.c | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/upb/encode.c b/upb/encode.c index a60d58a96e..ff84275ffb 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -449,7 +449,7 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg, return ch != 0; } #if UINTPTR_MAX == 0xffffffff - case upb_FieldRep_Pointer: + case kUpb_FieldRep_Pointer: #endif case kUpb_FieldRep_4Byte: { uint32_t u32; diff --git a/upb/mini_table.c b/upb/mini_table.c index 44fc5c7ff3..e48fb675f2 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -561,7 +561,7 @@ static void upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr, size_t len, uint32_t last_field_number = 0; bool need_dense_below = d->table != NULL; - d->end = ptr + len; + d->end = UPB_PTRADD(ptr, len); while (ptr < d->end) { char ch = *ptr++; @@ -650,8 +650,10 @@ static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { upb_MtDecoder_PushItem(d, item); } - qsort(d->vec.data, d->vec.size, sizeof(*d->vec.data), - upb_MtDecoder_CompareFields); + if (d->vec.size) { + qsort(d->vec.data, d->vec.size, sizeof(*d->vec.data), + upb_MtDecoder_CompareFields); + } return true; } @@ -735,7 +737,7 @@ size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) { } static bool upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { - upb_LayoutItem* end = d->vec.data + d->vec.size; + upb_LayoutItem* end = UPB_PTRADD(d->vec.data, d->vec.size); // Compute offsets. for (upb_LayoutItem* item = d->vec.data; item < end; item++) {