Skip to content

Commit dfc7524

Browse files
committed
Validate troop limit commands
1 parent 37e4166 commit dfc7524

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

libs/s25main/buildings/nobMilitary.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,10 @@ unsigned nobMilitary::CalcRequiredNumTroops(FrontierDistance assumedFrontierDist
661661

662662
void nobMilitary::SetTroopLimit(const unsigned rank, const unsigned limit)
663663
{
664-
troop_limits[rank] = limit;
664+
if(rank >= NUM_SOLDIER_RANKS)
665+
return;
666+
667+
troop_limits[rank] = std::min<unsigned>(limit, GetMaxTroopsCt());
665668
RegulateTroops();
666669
}
667670

tests/s25Main/integration/testAttacking.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,17 @@ BOOST_FIXTURE_TEST_CASE(StartAttack, AttackFixture<>)
418418
TestFailingAttack(gwv, milBld1Pos, attackSrc, 1u);
419419
}
420420

421+
BOOST_FIXTURE_TEST_CASE(TroopLimitCommandClampsCountAndIgnoresInvalidRank, AttackFixture<>)
422+
{
423+
const unsigned maxTroops = milBld0->GetMaxTroopsCt();
424+
425+
this->SetTroopLimit(milBld0Pos, 0, maxTroops + 10);
426+
BOOST_TEST_REQUIRE(milBld0->GetTroopLimit(0) == maxTroops);
427+
428+
this->SetTroopLimit(milBld0Pos, NUM_SOLDIER_RANKS, 0);
429+
BOOST_TEST_REQUIRE(milBld0->GetTroopLimit(0) == maxTroops);
430+
}
431+
421432
BOOST_FIXTURE_TEST_CASE(ConquerBld, AttackFixture<>)
422433
{
423434
AddSoldiers(milBld0Pos, 1, 5);

0 commit comments

Comments
 (0)