From 1b040c1fb5115ce2c59dad83499520f789a90e31 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 27 Sep 2026 13:18:45 -0400 Subject: [PATCH] Guard optional environment values explicitly in tests --- docs/engineering/VALIDATION.md | 15 +++++++++++++++ tests/environment_views_tests.cpp | 23 ++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 0543baf..7807f85 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1876,3 +1876,18 @@ Linux/UBSan/TSan or cluster result is claimed. Initial native static analysis flagged an unspecified enum base and integer widening in a size constant; both corrected before the passing analysis runs. No disk/host-loss recovery, biological maintenance, reproduction or player/offline progression added. + +## Environment-view optional guards - 2026-09-27 + +The supplied Linux clang-tidy-19 log rejected five optional field accesses in +`tests/environment_views_tests.cpp` with bugprone-unchecked-optional-access. Three +had no presence check; two relied on CHECK, whose throwing helper is not recognized +as a control-flow guard by this analysis. All five now use required_field, which +explicitly throws on absence before returning a reference. The rule remains enabled; +production code, formats, expected values and NDEBUG behavior are unchanged. + +Executed native analysis, Release and ASan builds plus the targeted +`environment-views-contracts` CTest in each preset; all passed. Release format check +and git diff --check passed. Full suites were not repeated for this test-only fix. +Linux clang-tidy/libstdc++ confirmation remains with the next Gitea run; native +analysis is not claimed as a reproduction of the Linux diagnostics. diff --git a/tests/environment_views_tests.cpp b/tests/environment_views_tests.cpp index d946a8a..9d79b7b 100644 --- a/tests/environment_views_tests.cpp +++ b/tests/environment_views_tests.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,14 @@ void check(bool ok, std::source_location at = std::source_location::current()) { throw std::runtime_error("environment view line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) + +const EnvironmentQuantity& +required_field(const std::optional& field, + std::source_location at = std::source_location::current()) { + if (!field.has_value()) + throw std::runtime_error("missing environment field at line " + std::to_string(at.line())); + return *field; +} template bool refused(const std::expected& r, E e) { return !r && r.error() == e; } @@ -74,11 +83,13 @@ void values_and_order() { CHECK(s.measure == HabitatMeasure{{200}, {600}}); CHECK(s.terrain.material() == MaterialId{1} && s.terrain.fill() == fill_scale); CHECK(!s.fields[1] && !s.fields[3]); // missing requested N; unrequested organic C - CHECK(std::get(*s.fields[2]) == EnvironmentMass{{0}, {0, 1}}); - CHECK(std::get(*s.fields[7]).value == 72); + CHECK(std::get(required_field(s.fields[2])) == + EnvironmentMass{{0}, {0, 1}}); + CHECK(std::get(required_field(s.fields[7])).value == 72); CHECK(s.chunk_revision == f.terrain->view(s.chunk)->revision()); } - CHECK(std::get(*q->samples()[0].fields[0]) == EnvironmentMass{{6}, {1, 100}}); + CHECK(std::get(required_field(q->samples()[0].fields[0])) == + EnvironmentMass{{6}, {1, 100}}); CHECK(q->samples()[0].moisture_status == MoistureStatus::available); CHECK(q->samples()[0].moisture == WaterSaturation{3, 50}); CHECK(q->samples()[1].habitat.substrate == MaterialId{2}); // no silent substrate reconciliation @@ -217,9 +228,11 @@ void maximum_footprint() { for (std::size_t k = 0; k < field_count; ++k) { CHECK(s.fields[k]); if (k < 7) - CHECK(std::get(*s.fields[k]) == EnvironmentMass{{4}, {4, 1}}); + CHECK(std::get(required_field(s.fields[k])) == + EnvironmentMass{{4}, {4, 1}}); else - CHECK(std::get(*s.fields[k]).value == 4); + CHECK(std::get(required_field(s.fields[k])).value == + 4); } CHECK(s.moisture == WaterSaturation{1, 25}); }