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; } 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");