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);
}
+33 -6
View File
@@ -93,8 +93,11 @@ Json yy_scalar(yyjson_val* v) {
}
}
/* Convert, counting against budget (containers after their children) and depth. Returns
* discarded when either bound trips after setting the matching flag. */
/* Convert, counting against budget (every value, containers included, as the pre-migration
* parser callback counted) and depth. Both bounds are enforced where the value is DISCOVERED,
* not in a second pass over the whole document: a body past either bound is refused while the
* scratch is still budget-sized, not after the tree's worth of frames has been collected.
* Returns discarded when either bound trips, after setting the matching flag. */
Json yy_convert(yyjson_val* root, int64_t budget, int max_depth, bool* over, bool* deep) {
*over = false;
*deep = false;
@@ -104,21 +107,36 @@ Json yy_convert(yyjson_val* root, int64_t budget, int max_depth, bool* over, boo
};
std::vector<Frame> order;
std::vector<Frame> stack;
int64_t seen = 1;
auto push = [&](yyjson_val* val, int depth) -> bool {
if (++seen > budget) {
*over = true;
return false;
}
if (depth > max_depth) {
*deep = true;
return false;
}
stack.push_back({ val, depth });
return true;
};
stack.push_back({ root, 0 });
while (!stack.empty()) {
const Frame f = stack.back();
stack.pop_back();
if (f.depth > max_depth) *deep = true;
if (f.depth > max_depth) { *deep = true; return Json(Json::value_t::discarded); }
order.push_back(f);
const yyjson_type t = yyjson_get_type(f.v);
if (t == YYJSON_TYPE_ARR) {
size_t idx, max;
yyjson_val* el;
yyjson_arr_foreach(f.v, idx, max, el) stack.push_back({ el, f.depth + 1 });
yyjson_arr_foreach(f.v, idx, max, el)
if (!push(el, f.depth + 1)) return Json(Json::value_t::discarded);
} else if (t == YYJSON_TYPE_OBJ) {
size_t idx, max;
yyjson_val *k, *val;
yyjson_obj_foreach(f.v, idx, max, k, val) stack.push_back({ val, f.depth + 1 });
yyjson_obj_foreach(f.v, idx, max, k, val)
if (!push(val, f.depth + 1)) return Json(Json::value_t::discarded);
}
}
if (*deep) return Json(Json::value_t::discarded);
@@ -216,7 +234,16 @@ Json Json::parse_bounded(std::string_view text, int64_t budget, int max_depth, b
YyDoc d(text, budget, false);
*over = false;
*deep = false;
if (!d.doc) return Json(value_t::discarded);
if (!d.doc) {
/* A syntax error comes back plain-discarded, like the parser's own refusal. But when
* the READ itself ran the arena out -- a well-formed document whose DOM outgrew the
* cap set at its open -- that IS the too-many-values refusal, reached one level below
* the discovery count (a 200k-empty-array swarm carries no scalar for the count to
* trip on, and yyjson refuses its DOM long before the converter sees a frame). Say
* so, so every caller's bound speaks with one voice. */
if (d.err.code == YYJSON_READ_ERROR_MEMORY_ALLOCATION) *over = true;
return Json(value_t::discarded);
}
Json out = yy_convert(yyjson_doc_get_root(d.doc), budget, max_depth, over, deep);
if (*over || *deep) return Json();
return out;
+10
View File
@@ -387,6 +387,16 @@ TEST(json_parse_errors_and_bounds) {
const Json ok = Json::parse_bounded("[[[1]]]", 100, 3, &over, &deep);
CHECK(!over && !deep && ok.is_array());
CHECK(Json::parse_bounded("{bad", 100, 3, &over, &deep).is_discarded());
/* A refused body must be refused WITHOUT first collecting it: this swarm carries no scalar
* at all, and the bounded read used to trip its budget in a second pass that ran only after
* every frame of the document sat in a scratch vector -- the refusal grew memory with the
* body, which is exactly what the bound promises against. Containers are values too. */
std::string swarm = "[";
for (int i = 0; i < 200000; ++i) swarm += "[],";
swarm.back() = ']';
over = deep = false;
CHECK(Json::parse_bounded(swarm, 1000, 64, &over, &deep).is_discarded());
CHECK(over && !deep);
}
TEST(json_parse_duplicate_keys_and_large_flat_objects) {