diag: the baseline and corpus files are read like the reports they are

Both files diagnose consumes are written by other runs and pasted
between people: --diagnose-baseline is an earlier report, the corpus a
forwarded file. Neither was read like a supplied file. The baseline got
the unbounded Json::parse, and compare_outputs then walked it with
throwing accessors -- a member it does not expect throws, and nothing
between run_diagnose and main catches -- so a crafted "earlier report"
aborted a run with std::terminate after hours of benchmarking work. The
corpus got a hand-rolled unbounded fread loop and a raw parser per
line: a deep line recursed the parse to a stack overflow before the
fingerprint check could ever run.

Now: capped read and parse_container on both (the corpus's size refusal
lands before the bytes are read), a per-line over/deep refusal that
names the line, and one try around the comparison so any structure the
comparison does not expect is the refusal it should be -- "the baseline
is not shaped like a report", parity not compared, the run keeps its
own results. diagnose_test pins the deep line.

Found by the 2.1.0 review pass (both security axes, independently).
This commit is contained in:
fl0l0u committed 2026-11-15 22:02:27 +00:00
1 parent 5c66eecc8e
commit d893fd05d1
3 files changed
+46 -18

No files matched your search

+28 -10
View File
@@ -652,19 +652,37 @@ int Engine::run_diagnose() {
bool parity_ok = true;
if (!cfg_.diagnose_baseline.empty()) {
std::ifstream f(cfg_.diagnose_baseline);
const json base = json::parse(f, nullptr, /*allow_exceptions=*/false);
/* A baseline is a report written by another run -- often on another machine, pasted
* between people: read it like a supplied file (capped read, bounded parse), and treat
* structure the comparison does not expect as a refusal, not as a crash. compare_outputs
* reads its baseline with throwing accessors, and nothing between here and main catches:
* a structurally hostile baseline used to abort a run that had already done its work. */
std::string why;
json cmp;
if (!f.is_open() || base.is_discarded()) {
std::string base_text;
const json base = read_text_file(cfg_.diagnose_baseline, base_text, kMaxContainerFile) == RAD_OK
? json::parse_container(base_text)
: json();
if (base.is_discarded()) {
why = "the baseline file is not readable JSON";
} else if (diag::compare_outputs(rep, base, &cmp, &why) == RAD_OK) {
cmp["baseline"] = "an earlier report";
rep["parity"] = cmp;
parity_ok = cmp["verdict"] == "identical" && cmp["acceptance_verdict"] == "no drop";
RAD_INFO("diagnose: parity %s, %lld of %lld requests identical; draft acceptance: %s",
cmp["verdict"].get<std::string>().c_str(), (long long)cmp["identical"].get<int64_t>(),
(long long)cmp["compared"].get<int64_t>(), cmp["acceptance_verdict"].get<std::string>().c_str());
} else {
int cmp_rc = RAD_E_FORMAT;
try {
cmp_rc = diag::compare_outputs(rep, base, &cmp, &why);
} catch (const json::exception& e) {
why = std::string("the baseline is not shaped like a report: ") + e.what();
cmp_rc = RAD_E_FORMAT;
}
if (cmp_rc == RAD_OK) {
cmp["baseline"] = "an earlier report";
rep["parity"] = cmp;
parity_ok = cmp["verdict"] == "identical" && cmp["acceptance_verdict"] == "no drop";
RAD_INFO("diagnose: parity %s, %lld of %lld requests identical; draft acceptance: %s",
cmp["verdict"].get<std::string>().c_str(),
(long long)cmp["identical"].get<int64_t>(),
(long long)cmp["compared"].get<int64_t>(),
cmp["acceptance_verdict"].get<std::string>().c_str());
}
}
if (!why.empty()) {
rep["parity"] = { { "baseline", "an earlier report" },