schemagen: reject scalar root values when root schema is object/array
CI / pre-commit (pull_request) Successful in 51s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-arm64, ubuntu-latest-arm64, true) (pull_request) Successful in 1m3s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-arm64, ubuntu-latest-arm64, false) (pull_request) Successful in 1m3s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-amd64, ubuntu-latest-amd64, true) (pull_request) Successful in 1m35s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-amd64, ubuntu-latest-amd64, false) (pull_request) Successful in 1m24s

The generated RootBuilder crashed (undefined behavior on std::vector::back())
when a JSON document's root value was a scalar or null while the schema
declared a non-nullable object or array root. The stack starts empty for
object/array roots, but cbStringData, cbNumberData, and cbBool called
stack_.back() without checking for an empty stack.

Add an empty-stack guard to the three scalar callbacks so they reject
instead of crashing. cbNull already handles the empty-stack case.

Regression tests added for:
- non-nullable object root rejecting null, boolean, number, and string roots
- nullable object root rejecting scalar roots
- nullable array root rejecting scalar roots

Closes #16
This commit is contained in:
2026-06-24 14:30:36 -04:00
parent e5cbaac401
commit df693ef4c9
3 changed files with 47 additions and 0 deletions
@@ -1098,6 +1098,7 @@ private:
void cbStringData(const char *buf, int len, int done) {{
if (error_) return;
if (stack_.empty()) {{ reject(); return; }}
Frame &f = stack_.back();
SlotInfo si = slotInfoG(f);
if (si.cat == Cat::Str) {{
@@ -1124,6 +1125,7 @@ private:
void cbNumberData(const char *buf, int len, int done) {{
if (error_) return;
if (stack_.empty()) {{ reject(); return; }}
Frame &f = stack_.back();
SlotInfo si = slotInfoG(f);
if (si.cat != Cat::Int && si.cat != Cat::Dbl) {{ reject(); return; }}
@@ -1157,6 +1159,7 @@ private:
void cbBool(bool value) {{
if (error_) return;
if (stack_.empty()) {{ reject(); return; }}
Frame &f = stack_.back();
SlotInfo si = slotInfoG(f);
if (si.cat != Cat::Bool) {{ reject(); return; }}