Skip to content

upb: handle unlinked submessages in upb_Message_Convert - #31876

Open
hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-convert-unlinked-submessages
Open

hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-convert-unlinked-submessages

Conversation

@hyc-alt

@hyc-alt hyc-alt commented Nov 27, 2026

Copy link
Copy Markdown

Summary

upb_Message_Convert() does not handle unlinked submessage fields (upb_MiniTable_SubMessage(dst_f) == NULL) on the destination upb_MiniTable, which occur legitimately in tree-shaken or dynamically built MiniTables. While upb_Decode() preserves unlinked submessage fields (and map fields whose value submessage is unlinked) as unknown fields (_upb_Decoder_CheckUnlinked, DecodeDynamicMapUnlinkedValue), upb_Message_Convert() crashes with NULL pointer dereferences or produces corrupted messages that crash upb_Encode():

  1. Scalar submessage (upb_Message_ConvertField): calls upb_Message_GetOrCreateMutableMessage() with dst_sub_mt == NULL -> debug UPB_ASSERT(sub_mini_table) failure and release SIGSEGV.
  2. Repeated submessage (upb_Message_ConvertArrayField / upb_Array_DeepConvert): falls through to the primitive else branch in upb_Array_DeepConvert(), copying source submessage pointers into an unlinked destination array -> debug round-trip assertion failure and SIGSEGV in upb_Encode().
  3. Map with unlinked value submessage (upb_Message_ConvertMapField / upb_Map_DeepConvert*): both int-key and string-key maps copy raw source submessage pointers into dst_map while dst_val_mt == NULL -> debug round-trip assertion failure and SIGSEGV in upb_Encode().
  4. Extension / unknown AuxField targeting an unlinked destination field (upb_Message_ConvertExtension): calls upb_Message_New(NULL, c->arena) for scalars (immediate UBSan NULL deref / SIGSEGV) or populates an unlinked array for repeated fields (SIGSEGV in upb_Encode()).

Fix

Align upb_Message_Convert() with upb_Decode()'s unlinked submessage behavior:

  • Scalar and repeated base submessages (dst_sub_mt == NULL): demote non-empty fields to unknown aux fields via upb_Message_DemoteToAuxField() (skipping empty arrays).
  • Map fields with unlinked value submessages (dst_val_mt == NULL): encode non-empty maps to unknown wire bytes via upb_Message_EncodeUnknownField() + upb_Message_FlushUnknownChunk() (skipping empty maps), matching DecodeDynamicMapUnlinkedValue.
  • Extensions and unknown aux fields targeting an unlinked base field (dst_sub_mt == NULL): preserve non-empty entries as unknown aux fields via a shared upb_Message_AddUnknownAuxField() helper.
  • Invalid inputs (populated unlinked src submessage or unlinked registered destination extension): fail cleanly with kUpb_ConvertStatus_Incompatible (returning NULL).

Verification

  • Before fix: All 6 cases (scalar, repeated, map<int32, SubMsg>, map<string, SubMsg>, AuxField scalar, AuxField repeated) fail debug assertions and crash with ASan SEGV + UBSan NULL dereference under -DNDEBUG -fsanitize=address,undefined.
  • After fix:
    • upb/message/convert.c compiles cleanly with -Wall -Wextra -Werror.
    • All 6 cases pass cleanly under Debug+ASan+UBSan (with #ifndef NDEBUG round-trip check), Release+ASan+UBSan (-DNDEBUG), Release (-O2 -DNDEBUG), and FastTable Release (-O2 -DNDEBUG -DUPB_ENABLE_FASTTABLE=1).
    • Added regression tests in upb/message/convert_test.cc: UnlinkedDestinationScalarSubmessage, UnlinkedDestinationRepeatedSubmessage, UnlinkedDestinationMapValueSubmessage, and UnlinkedDestinationAuxFieldSubmessages.

upb_Message_Convert() did not handle unlinked destination submessage
fields (upb_MiniTable_SubMessage(dst_f) == NULL), causing NULL pointer
dereferences or corrupted destination messages that crash upb_Encode():

- Scalar submessages called upb_Message_GetOrCreateMutableMessage() with
  a NULL sub-MiniTable (assert in debug, SIGSEGV in release).
- Repeated submessages fell through to the primitive branch of
  upb_Array_DeepConvert(), copying source pointers into an unlinked
  destination array.
- Map fields with unlinked value submessages copied source submessage
  pointers into dst_map instead of encoding entries as unknown fields.
- Extensions and unknown aux fields targeting unlinked destination fields
  called upb_Message_New(NULL) for scalars or populated unlinked arrays
  for repeated fields.

Preserve non-empty unlinked submessages as unknown aux fields (or encoded
unknown bytes for maps), matching upb_Decode() semantics, and fail cleanly
with kUpb_ConvertStatus_Incompatible on invalid unlinked source fields or
unlinked registered destination extensions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant