From af0217261a538e1f8a62f399e158b0a88b422afb Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 20 Nov 2026 03:00:54 -0700 Subject: [PATCH 1/2] test: cover lagging id counters and a 64-bit stockpile on load These cases fail against the current fromWorld: a save whose nextSettler/nextBuilding counter is at or behind its ids (or negative) loads, and a stockpile 2^32 off the map wraps back onto it. Pushed ahead of the fix so CI records the failure. Co-Authored-By: Claude Opus 5.5 --- tests/test_saves.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test_saves.cpp b/tests/test_saves.cpp index a38cd74..de9ba0d 100644 --- a/tests/test_saves.cpp +++ b/tests/test_saves.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "bridge/bridge.h" #include "bridge/session.h" @@ -75,6 +76,43 @@ int main(int argc, char** argv) { bad.tables[0].ints[field][0] = -1; CHECK(!bridge::fromWorld(bad, back, &why)); } + // Id counters must stay ahead of the ids, or the first birth or hut throws. + CHECK(c.settlers.size() > 0 && c.buildings.size() > 0); + const std::pair lagging[] = { + {"colony:nextSettler", c.settlers.id.back()}, // equal to the last settler id + {"colony:nextSettler", c.settlers.id.back() - 1}, // behind it + {"colony:nextBuilding", c.buildings.id.back()}, + {"colony:nextBuilding", -1}, + }; + for (const auto& [key, value] : lagging) { + bad = bridge::toWorld(c); + bad.globals[key] = value; + why.clear(); + CHECK(!bridge::fromWorld(bad, back, &why)); + CHECK_MSG(why == "id counter behind ids", why); + } + { // with no settlers left, a negative counter would still hand out a negative id + Colony empty = c; + while (empty.settlers.size() > 0) empty.settlers.remove(empty.settlers.size() - 1); + CHECK_MSG(bridge::fromWorld(bridge::toWorld(empty), back, &why), why); + bad = bridge::toWorld(empty); + bad.globals["colony:nextSettler"] = -5; + why.clear(); + CHECK(!bridge::fromWorld(bad, back, &why)); + CHECK_MSG(why == "id counter behind ids", why); + } + // The stockpile is checked at its full 64 bits: off the map, and off the map by + // exactly 2^32 (which a 32-bit cast would wrap back onto the map). + for (const char* key : {"colony:stockX", "colony:stockY"}) { + const int64_t onMap = std::string(key) == "colony:stockX" ? c.globals.stockX : c.globals.stockY; + for (int64_t value : {int64_t{9999}, onMap + (int64_t{1} << 32), onMap - (int64_t{1} << 32)}) { + bad = bridge::toWorld(c); + bad.globals[key] = value; + why.clear(); + CHECK(!bridge::fromWorld(bad, back, &why)); + CHECK_MSG(why == "stockpile out of bounds", why); + } + } } SECTION("1,000-tick soak: save every 100 ticks, reload, exact, keep playing from the reload"); From 3ed06c484101a546de8d28f03f5569e7722c4de8 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 20 Nov 2026 03:06:18 -0700 Subject: [PATCH 2/2] fix: refuse a save with lagging id counters or a 64-bit stockpile fromWorld now refuses a save whose nextSettler/nextBuilding counter is negative or not greater than the last id in its table ("id counter behind ids"), which would otherwise load and then throw at the first birth or hut. The stockpile position is checked with fits() before its 32-bit cast, so a value 2^32 off the map no longer wraps back onto it. The save format is unchanged. Closes #29 Closes #31 Co-Authored-By: Claude Opus 5.5 --- src/bridge/bridge.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bridge/bridge.cpp b/src/bridge/bridge.cpp index 6915916..f9af9b6 100644 --- a/src/bridge/bridge.cpp +++ b/src/bridge/bridge.cpp @@ -151,8 +151,15 @@ bool fromWorld(const World& w, Colony& out, std::string* err) { return setErr(err, "settler state out of range"), false; for (size_t i = 0; i < c.buildings.size(); ++i) if (!c.grid.inBounds(c.buildings.x[i], c.buildings.y[i])) return setErr(err, "building out of bounds"), false; - if (!c.grid.inBounds(static_cast(c.globals.stockX), static_cast(c.globals.stockY))) + if (!fits(c.globals.stockX) || !fits(c.globals.stockY) || + !c.grid.inBounds(static_cast(c.globals.stockX), static_cast(c.globals.stockY))) return setErr(err, "stockpile out of bounds"), false; + // Settlers::add and Buildings::add throw unless each new id exceeds the last one. + auto ahead = [](int64_t next, const std::vector& ids) { + return next >= 0 && (ids.empty() || next > ids.back()); + }; + if (!ahead(c.globals.nextSettlerId, c.settlers.id) || !ahead(c.globals.nextBuildingId, c.buildings.id)) + return setErr(err, "id counter behind ids"), false; out = std::move(c); return true; }