Skip to content

upb: return NULL on miss in upb_DefPool_FindExtensionByMiniTable and skip in upb_Message_Next - #31879

Open
hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-find-extension-by-minitable-uninit
Open

hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-find-extension-by-minitable-uninit

Conversation

@hyc-alt

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

Copy link
Copy Markdown

Summary

Fixes an uninitialized stack pointer return in upb_DefPool_FindExtensionByMiniTable() (upb/reflection/def_pool.c) and a missing NULL check in upb_Message_Next() (upb/reflection/message.c).

Root Cause

  1. upb_DefPool_FindExtensionByMiniTable() (upb/reflection/def_pool.c:622-628) declares an uninitialized upb_value v, calls upb_inttable_lookup(&s->exts, (uintptr_t)ext, &v), asserts ok, and unconditionally returns upb_value_getconstptr(v). When ext is not registered in s:
    • Debug builds abort on UPB_ASSERT(ok).
    • Release (-DNDEBUG) builds return an uninitialized stack value cast to const upb_FieldDef*.
  2. upb_Message_Next() (upb/reflection/message.c:180-191) is explicitly documented in upb/reflection/message.h:70-72 to skip extensions that do not match the provided ext_pool:

    If ext_pool is NULL, no extensions will be returned. If the given symtab returns extensions that don't match what is in this message, those extensions will be skipped.
    Instead, it passes the message's canonical extension directly to upb_DefPool_FindExtensionByMiniTable() without checking for NULL, causing wild-pointer dereferences (SIGSEGV) in upb_Message_Next(), upb_TextEncode(), upb_JsonEncode(), upb_Message_DiscardUnknown(), and upb_util_HasUnsetRequired().

Fix

  • Return NULL from upb_DefPool_FindExtensionByMiniTable() when upb_inttable_lookup() misses.
  • In upb_Message_Next(), check the returned const upb_FieldDef* and continue past extensions missing from ext_pool.

Testing

  • Added regression test MessageTest.MessageNextSkipsExtensionsMissingFromExtPool in upb/message/test.cc covering direct upb_DefPool_FindExtensionByMiniTable(), upb_Message_Next(), upb_JsonEncode(), and upb_Message_DiscardUnknown().
  • Verified before/after under ASan+UBSan in both Release (-DNDEBUG) and Debug builds as well as -std=c99 -Wall -Wextra -Werror.

…skip in upb_Message_Next

upb_DefPool_FindExtensionByMiniTable() declares an uninitialized `upb_value v`,
calls upb_inttable_lookup(&s->exts, (uintptr_t)ext, &v), asserts `ok`, and
unconditionally returns upb_value_getconstptr(v). When `ext` is not registered
in `s`:
- In debug builds, UPB_ASSERT(ok) aborts.
- In release (-DNDEBUG) builds, `v` is never written and the function returns
  an uninitialized stack value cast to `const upb_FieldDef*`.

This directly breaks the documented contract of upb_Message_Next() in
upb/reflection/message.h ("If the given symtab returns extensions that don't
match what is in this message, those extensions will be skipped"), causing
wild-pointer dereferences (SIGSEGV) in callers such as upb_Message_Next(),
upb_TextEncode(), upb_JsonEncode(), upb_Message_DiscardUnknown(), and
upb_util_HasUnsetRequired().

Return NULL from upb_DefPool_FindExtensionByMiniTable() when the lookup misses,
and skip entries where upb_DefPool_FindExtensionByMiniTable() returns NULL in
upb_Message_Next().

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