util: the bounded read refuses before the tree exists

yy_convert enforced its value budget and depth bound in a second pass
that ran only after every frame of the document had been collected into
a scratch vector. The bound that promises a refused body has not grown
memory grew its scratch with the body: a 200k-empty-array swarm counts
no scalar at all, and the old code refused it after the whole swarm sat
in the frame list.

Both bounds are now enforced where a value is DISCOVERED -- every value
counted, containers included, which is what the pre-migration parser
callback counted and what the tests already pinned -- so a body past
either bound ends the walk while the scratch is still budget-sized. What
stays proportional to the body is yyjson's own read arena, capped by the
byte bound at its open, and oai.cpp's comment now says exactly that. A
json_test case carries the swarm.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:09 +00:00
1 parent 7b9f33653f
commit e46a8ba3d4
3 files changed
+47 -9

No files matched your search

+4 -3
View File
@@ -142,9 +142,10 @@ int64_t json_value_budget(int64_t max_ctx) {
* copy recurse once per level, and a request's schema is serialised on the way to its grammar.
* A few hundred KB of brackets, well inside the value budget, would overflow the worker's stack.
* No legitimate request nests near it; the grammar compiler refuses schemas at half the bound. */
/* The bounded read itself lives in util/json.cpp, where yyjson's transient allocation is
* capped in bytes as the value count is capped here, so a body that is refused has also not
* been allowed to grow memory on its way to the refusal. */
/* The bounded read itself lives in util/json.cpp. Both of its bounds -- the value count here,
* the reader's byte cap -- are enforced as the document is discovered, so a body refused for
* size or nesting is refused while the conversion's scratch is still budget-sized; what stays
* proportional to the body is yyjson's own read arena, capped by the byte bound at its open. */
json parse_json_bounded(const std::string& body, int64_t max_ctx, bool* over, bool* deep) {
return Json::parse_bounded(body, json_value_budget(max_ctx), kJsonMaxDepth, over, deep);
}