Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Sim: immutable copies for clone-shared lists, refusal test checks the state (Fixes #2513) (#2557)

Beri02b3fa3b0f61
diff --git a/changelog.d/2513-clone-shared-arrays.md b/changelog.d/2513-clone-shared-arrays.md
new file mode 100644
index 000000000..87426d7a7
--- /dev/null
+++ b/changelog.d/2513-clone-shared-arrays.md
@@ -0,0 +1,2 @@
+- Sim hardening (Refs #2513): a qualified halutz list and a corridor's segment list can no longer be written through
+ from a cloned game state, so a clone cannot change the original's hash. No change to play.
diff --git a/sim/Statecraft.Sim.Tests/Gdd03HousingRepairTests.cs b/sim/Statecraft.Sim.Tests/Gdd03HousingRepairTests.cs
index 0f9692638..e863a1438 100644
--- a/sim/Statecraft.Sim.Tests/Gdd03HousingRepairTests.cs
+++ b/sim/Statecraft.Sim.Tests/Gdd03HousingRepairTests.cs
@@ -143,6 +143,8 @@ public class Gdd03HousingRepairTests
Assert.Equal(Repair, ((StartProject)refused.Command).ProjectKind);
Assert.Equal("Town A has started a city project this turn", refused.Reason);
Assert.Single(both.Why, w => w.Key == CauseKeys.Paid && w.Args[0] == CauseKeys.Purpose.Project);
+ // The refused repair changed nothing: the state is as after the first start alone (as CityStartCapTests does for R-PROJ-001).
+ Assert.Equal(Step(c, s, new StartProject("housing", "town_a", 1, 1)).State.Hash(), both.State.Hash());
var full = s.Clone();
full.MapActionsTurnDate = full.Date; full.MapActionsThisTurn = c.RulesAt(full.Date).MapActionsPerTurn;
diff --git a/sim/Statecraft.Sim.Tests/QualificationTests.cs b/sim/Statecraft.Sim.Tests/QualificationTests.cs
index 74dfc2487..e5ecd261f 100644
--- a/sim/Statecraft.Sim.Tests/QualificationTests.cs
+++ b/sim/Statecraft.Sim.Tests/QualificationTests.cs
@@ -398,4 +398,20 @@ public class QualificationTests
Assert.Empty(halutz.Rejected);
Assert.Equal(new[] { "1", "halutz" }, Assert.Single(halutz.Why, x => x.Key == "pioneers_reassigned").Args);
}
+
+ // #2513: a clone shares its records, so a record's lists must not be arrays a cast could write through. Writing through
+ // whatever the list is backed by, on the clone, leaves the original's hash alone.
+ [Fact]
+ public void ACloneCannotWriteThroughTheListsItSharesWithTheOriginal()
+ {
+ var c = Town();
+ var s = WithCity(c, new City("town_a", 12, 8, 4, 0, 0, qualified: new Qualified(0, 0, new[] { 1, 2 })));
+ s.Corridors["road_a"] = new CorridorWork("road_a", new SegmentWork?[] { new SegmentWork(null, 6, new DateTime(1948, 5, 15)), null });
+ var before = s.Hash();
+ var clone = s.Clone();
+ Assert.Throws<NotSupportedException>(() => ((IList<int>)clone.Cities["town_a"].Qualified.Halutzim)[0] = 9);
+ Assert.Throws<NotSupportedException>(() => ((IList<SegmentWork?>)clone.Corridors["road_a"].Segments)[0] = null);
+ Assert.Equal(before, s.Hash());
+ Assert.Equal(before, clone.Hash());
+ }
}
diff --git a/sim/Statecraft.Sim/GddState.cs b/sim/Statecraft.Sim/GddState.cs
index 82644b538..0151bee2f 100644
--- a/sim/Statecraft.Sim/GddState.cs
+++ b/sim/Statecraft.Sim/GddState.cs
@@ -408,7 +408,7 @@ namespace Statecraft.Sim
public string Id { get; }
public IReadOnlyList<SegmentWork?> Segments { get; }
- public CorridorWork(string id, SegmentWork?[] segments) { Id = id; Segments = (SegmentWork?[])segments.Clone(); }
+ public CorridorWork(string id, SegmentWork?[] segments) { Id = id; Segments = Array.AsReadOnly((SegmentWork?[])segments.Clone()); }
/// <summary>The same corridor with segment <paramref name="index"/> (0-based) replaced; null when no segment is left
/// with a record.</summary>
diff --git a/sim/Statecraft.Sim/Qualification.cs b/sim/Statecraft.Sim/Qualification.cs
index b407bcc4d..887ad3770 100644
--- a/sim/Statecraft.Sim/Qualification.cs
+++ b/sim/Statecraft.Sim/Qualification.cs
@@ -20,7 +20,7 @@ namespace Statecraft.Sim
public Qualified(int craftsmen, int veterans, IReadOnlyList<int> halutzim)
{
- Craftsmen = craftsmen; Veterans = veterans; Halutzim = halutzim.OrderBy(x => x).ToArray();
+ Craftsmen = craftsmen; Veterans = veterans; Halutzim = Array.AsReadOnly(halutzim.OrderBy(x => x).ToArray());
}
public int Total => Craftsmen + Veterans + Halutzim.Count;