schemagen: reject scalar root values when root schema is object/array #32

Merged
andrew merged 1 commits from weaselbot/weaseljson:weaselbot/issue-16 into main 2026-06-24 19:34:07 +00:00
Member

Closes #16

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 correctly.

Regression tests added for:

  • non-nullable object root rejecting null, true, 123, and "hi" roots
  • nullable object root rejecting scalar roots
  • nullable array root rejecting scalar roots
Closes #16 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 correctly. Regression tests added for: - non-nullable object root rejecting `null`, `true`, `123`, and `"hi"` roots - nullable object root rejecting scalar roots - nullable array root rejecting scalar roots
weaselbot added 1 commit 2026-06-24 18:30:50 +00:00
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
df693ef4c9
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
andrew merged commit 4ce2002663 into main 2026-06-24 19:34:07 +00:00
andrew deleted branch weaselbot/issue-16 2026-06-24 19:34:07 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: weaselab/weaseljson#32