util: one wording for the bounded read's refusal

The refuse sites that read a container file all re-typed the same
two-branch message -- over ? "carries far too many values"
: "nests far too deep" -- six times, and the copies had already
drifted: gen_config said "carries far too many JSON values", xstats
"far too many values", tokenizer_config "it does not parse" for the
discarded case. The bounds themselves (2^24, 256) were also spelled
as literals in tokenizer_json's refusal, so a bound change there
would make its message a lie.

Json::bound_why(over, deep) is the phrase, owned beside the read that
sets the flags, and the two bounds are exported as kContainerBudget /
kContainerDepth so the one message that states them reads them from
the constants. Every refuse site now calls it; the wording is one, the
numbers cannot drift. No behaviour change -- the same flags map to the
same failure paths, only the text they print is unified.
This commit is contained in:
fl0l0u committed 2026-11-15 22:02:29 +00:00
1 parent 5e2e596644
commit b9ecbde0d6
6 files changed
+23 -14

No files matched your search

+2 -4
View File
@@ -99,8 +99,7 @@ bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampli
bool over = false, deep = false;
Json j = Json::parse_container(json_text, &over, &deep);
if (over || deep) {
RAD_WARN("%s: %s; ignoring it", src,
over ? "carries far too many JSON values" : "nests far too deep");
RAD_WARN("%s: %s; ignoring it", src, Json::bound_why(over, deep));
return false;
}
if (j.is_discarded()) { RAD_WARN("%s: not valid JSON; ignoring it", src); return false; }
@@ -222,8 +221,7 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
if (over || deep)
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
"array of ids (%s); ignoring it",
k, log_sanitize(v).c_str(),
over ? "it carries far too many values" : "it nests far too deep");
k, log_sanitize(v).c_str(), Json::bound_why(over, deep));
else if (!w.is_discarded()) j[k] = std::move(w);
else
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
+1 -2
View File
@@ -815,8 +815,7 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
bool over = false, deep = false;
const ojson cfg = ojson::parse_container(text, &over, &deep);
if (over || deep)
return fail(err, RAD_E_FORMAT,
cfg_path + (over ? ": carries far too many JSON values" : ": nests far too deep"));
return fail(err, RAD_E_FORMAT, cfg_path + ": " + ojson::bound_why(over, deep));
if (cfg.is_discarded() || !cfg.is_object())
return fail(err, RAD_E_FORMAT, cfg_path + ": not a JSON object");
auto truthy = [&](const char* k) {
+1 -1
View File
@@ -541,7 +541,7 @@ int xs_profile_load(const std::string& path, std::vector<RadFileProfile>* out, i
Json j = Json::parse_container(text, &over, &deep);
if (over || deep || j.is_discarded()) {
RAD_ERR("--expert-profile: %s is not JSON (%s)", path.c_str(),
over ? "far too many values" : deep ? "nests far too deep" : "malformed");
Json::bound_why(over, deep));
return RAD_E_INVAL;
}
if (!j.is_object() || j.value("format", "") != "radiance-expert-stats/1" ||
+5 -6
View File
@@ -587,9 +587,7 @@ void read_config_json(const std::string& dir, VocabBuild& out) {
json c = json::parse_container(text, &over, &deep);
if (over || deep || c.is_discarded()) {
RAD_WARN("tokenizer: %s did not read as a bounded JSON document (%s); ids left unset",
path.c_str(),
over ? "it carries far too many values"
: deep ? "it nests far too deep" : "it does not parse");
path.c_str(), json::bound_why(over, deep));
return;
}
std::unordered_map<std::string, int32_t> id_of;
@@ -793,9 +791,10 @@ int parse_tokenizer_json_buf(std::string_view text, const char* origin, VocabBui
bool over = false, deep = false;
json j = json::parse_container(text, &over, &deep);
if (over || deep) {
RAD_ERR("%s: %s; a tokenizer file may not carry more than 2^24 values or nest more "
"than 256 levels",
origin, over ? "carries far too many values" : "nests far too deep");
RAD_ERR("%s: %s; a tokenizer file may not carry more than %lld values or nest more "
"than %d levels",
origin, json::bound_why(over, deep),
(long long)json::kContainerBudget, json::kContainerDepth);
return RAD_E_FORMAT;
}
if (j.is_discarded()) {
+7 -1
View File
@@ -251,12 +251,18 @@ Json Json::parse_bounded(std::string_view text, int64_t budget, int max_depth, b
Json Json::parse_container(std::string_view text, bool* over, bool* deep) {
bool o = false, d = false;
Json j = parse_bounded(text, 1LL << 24, 256, &o, &d);
Json j = parse_bounded(text, kContainerBudget, kContainerDepth, &o, &d);
if (over) *over = o;
if (deep) *deep = d;
return j;
}
const char* Json::bound_why(bool over, bool deep) {
return over ? "carries far too many values"
: deep ? "nests far too deep"
: "is not valid JSON";
}
Json Json::parse(std::string_view text) { return parse(text, nullptr, true, false); }
Json Json::parse(std::istream& in) {
+7
View File
@@ -901,6 +901,13 @@ public:
* to overflow a dump's or a destructor's stack on the way there. */
static Json parse_container(std::string_view text, bool* over = nullptr,
bool* deep = nullptr);
/* The refusal message behind `over`/`deep`: one wording for all callers, so the bound and
* the phrase that names it live together instead of being re-typed at each refuse site
* (six near-copies had drifted). The phrase for whichever bound tripped. */
static const char* bound_why(bool over, bool deep);
/* The container read's own bounds, for a caller that states them in its refusal. */
static constexpr int64_t kContainerBudget = 1LL << 24;
static constexpr int kContainerDepth = 256;
private:
using difference_type = std::ptrdiff_t;