From f66a3e726f097841895ef003f64130f68932e950 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Mon, 14 Nov 2011 22:22:38 +0000 Subject: [PATCH] Avoid overflow identified by /RTCc warning This was SVN commit r10537. --- source/simulation2/components/CCmpRangeManager.cpp | 10 ++++++++-- .../simulation2/components/tests/test_RangeManager.h | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/simulation2/components/CCmpRangeManager.cpp b/source/simulation2/components/CCmpRangeManager.cpp index 0a7c172f64..28581e0995 100644 --- a/source/simulation2/components/CCmpRangeManager.cpp +++ b/source/simulation2/components/CCmpRangeManager.cpp @@ -1028,6 +1028,7 @@ public: m_LosState[idx] |= ((LOS_VISIBLE | LOS_EXPLORED) << (2*(owner-1))); } + ASSERT(counts[idx] < 65535); counts[idx] = (u16)(counts[idx] + 1); // ignore overflow; the player should never have 64K units } } @@ -1045,6 +1046,7 @@ public: for (i32 idx = idx0; idx <= idx1; ++idx) { + ASSERT(counts[idx] > 0); counts[idx] = (u16)(counts[idx] - 1); // Decreasing from non-zero to zero - move from visible+explored to explored @@ -1263,10 +1265,14 @@ public: // Process each of the regions as its own strip. // (If this produces negative-width strips then they'll just get ignored // which is fine.) - LosRemoveStripHelper(owner, i0clamp_from, i0clamp_to-1, j, countsData); - LosRemoveStripHelper(owner, i1clamp_to+1, i1clamp_from, j, countsData); + // (If the strips don't actually overlap (which is very rare with normal unit + // movement speeds), the region between them will be both added and removed, + // so we have to do the add first to avoid overflowing to -1 and triggering + // assertion failures.) LosAddStripHelper(owner, i0clamp_to, i0clamp_from-1, j, countsData); LosAddStripHelper(owner, i1clamp_from+1, i1clamp_to, j, countsData); + LosRemoveStripHelper(owner, i0clamp_from, i0clamp_to-1, j, countsData); + LosRemoveStripHelper(owner, i1clamp_to+1, i1clamp_from, j, countsData); } } } diff --git a/source/simulation2/components/tests/test_RangeManager.h b/source/simulation2/components/tests/test_RangeManager.h index 1eca49344f..77b88799c5 100644 --- a/source/simulation2/components/tests/test_RangeManager.h +++ b/source/simulation2/components/tests/test_RangeManager.h @@ -110,6 +110,11 @@ public: { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(256), entity_pos_t::FromInt(256)-entity_pos_t::Epsilon(), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); + { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(383), entity_pos_t::FromInt(84), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } + cmp->Verify(); + { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(348), entity_pos_t::FromInt(83), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } + cmp->Verify(); + WELL512 rng; for (size_t i = 0; i < 1024; ++i) {