Skip to content

fix: guard against null prev_val in JsonPrinter::PrintOffset for unions (fixes #9033) - #9236

Open
jdymitarai wants to merge 1 commit into
google:masterfrom
jdymitarai:fix-union-null-deref-print-offset
Open

fix: guard against null prev_val in JsonPrinter::PrintOffset for unions (fixes #9033)#9236
jdymitarai wants to merge 1 commit into
google:masterfrom
jdymitarai:fix-union-null-deref-print-offset

Conversation

@jdymitarai

Copy link
Copy Markdown

Summary of Changes

Fixes #9033 (CWE-476 Null Pointer Dereference in JsonPrinter::PrintOffset() for union types).

Root Cause

In src/idl_gen_text.cpp, JsonPrinter::PrintOffset() dereferences prev_val to obtain the union type discriminator:

case BASE_TYPE_UNION: {
  FLATBUFFERS_ASSERT(prev_val);
  auto union_type_byte = *prev_val;

In release builds (-DNDEBUG), FLATBUFFERS_ASSERT is removed. When processing an untrusted or malformed FlatBuffer where the union value field is present in the table vtable but the union type discriminator is absent (vtable offset zeroed), prev_val is nullptr. Attempting *prev_val immediately causes a segmentation fault (SIGSEGV), terminating flatc or any library converting unverified/corrupted binary buffers to JSON.

Additionally, for vectors of unions (vector_index >= 0), if vector_index >= type_vec->size(), accessing type_vec->Get(vector_index) can result in an out-of-bounds read.

Fix

  1. Replace debug-only FLATBUFFERS_ASSERT(prev_val) with a runtime check returning "corrupt buffer: missing union type field".
  2. Check vector bounds (static_cast<uoffset_t>(vector_index) >= type_vec->size()) returning "corrupt buffer: union type vector out of range".
  3. Add a unit test in tests/test.cpp (UnionVectorTest) that crafts a corrupted vtable with missing union type discriminator and verifies that GenText() returns an error string instead of crashing.

…ns (fixes google#9033)

In JsonPrinter::PrintOffset(), union fields require reading the union type discriminator
byte via `prev_val`. Previously, the code only checked `FLATBUFFERS_ASSERT(prev_val)`.
In release builds (-DNDEBUG), asserts are compiled out. If a malformed or corrupt buffer
has the union value field present in the vtable but the union type discriminator absent,
`prev_val` is nullptr, causing an immediate segmentation fault (CWE-476).

Similarly, if vector_index is out of range for the union type vector, it could lead to an
out-of-bounds read.

This commit adds runtime validation for `prev_val` and vector bounds before dereferencing,
returning descriptive error messages and propagating them safely through GenText().
Also adds a unit test in tests/test.cpp to ensure corrupt buffers are rejected cleanly.

Fixes google#9033.
@github-actions github-actions Bot added c++ codegen Involving generating code from schema labels Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null Pointer Dereference in JsonPrinter::PrintOffset() for Union Types

1 participant