-
Notifications
You must be signed in to change notification settings - Fork 234
bugfix(controlbar): Fix possible divisions by zero in Control Bar code #2867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2702,8 +2702,7 @@ void Player::resetRank() | |
| m_rankLevel = 1; | ||
| m_skillPoints = 0; | ||
| const RankInfo* nextRank = TheRankInfoStore->getRankInfo(m_rankLevel+1); | ||
| m_levelUp = nextRank ? nextRank->m_skillPointsNeeded : INT_MAX; | ||
| m_levelDown = 0; | ||
| setSafeLevels(nextRank ? nextRank->m_skillPointsNeeded : INT_MAX, 0); | ||
| m_sciences.clear(); | ||
| m_sciencePurchasePoints = getPlayerTemplate() ? getPlayerTemplate()->getIntrinsicSciencePurchasePoints() : 0; | ||
| const RankInfo* curRank = TheRankInfoStore->getRankInfo(m_rankLevel); | ||
|
|
@@ -2762,7 +2761,8 @@ Bool Player::setRankLevel(Int newLevel) | |
| } | ||
|
|
||
| const RankInfo* nextRank = TheRankInfoStore->getRankInfo(newLevel + 1); | ||
| m_levelUp = nextRank ? nextRank->m_skillPointsNeeded : INT_MAX; | ||
| setSafeLevels(nextRank ? nextRank->m_skillPointsNeeded : INT_MAX, m_levelDown); | ||
|
|
||
| m_rankLevel = newLevel; | ||
|
|
||
| DEBUG_ASSERTCRASH(m_skillPoints >= m_levelDown && m_skillPoints < m_levelUp, ("hmm, wrong")); | ||
|
|
@@ -2788,6 +2788,22 @@ Bool Player::setRankLevel(Int newLevel) | |
| return true; | ||
| } | ||
|
|
||
| //============================================================================= | ||
| void Player::setSafeLevels(Int levelUp, Int levelDown) | ||
| { | ||
| if (levelUp == levelDown) | ||
| { | ||
| // TheSuperHackers @bugfix Prevent possible division by zero in the control bar code. | ||
| m_levelUp = INT_MAX; | ||
| m_levelDown = 0; | ||
| } | ||
|
Comment on lines
+2794
to
+2799
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Equal adjacent Prompt To Fix With AIThis is a comment left during a code review.
Path: GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
Line: 2794-2799
Comment:
**Do not terminalize duplicates**
Equal adjacent `SkillPointsNeeded` values are valid custom-rank input for the crash case this code handles. When a player reaches rank N and rank N+1 has the same threshold, this branch replaces the next threshold with `INT_MAX`; `addSkillPoints()` can then never advance the player to rank N+1 even though the threshold is already met. Handle a zero-width transition without converting it into the terminal-rank sentinel.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. setRankLevel() first assigns m_levelDown from the newly reached rank (Player.cpp:2759). It then reads the next rank and passes its threshold together with that lower bound to setSafeLevels() at line 2764. When adjacent ranks have the same accepted SkillPointsNeeded, those are two legitimate equal bounds. setSafeLevels() replaces them with INT_MAX/0 at lines 2794–2799, so addSkillPoints() can no longer continue through the equal-threshold rank as its existing loop intends. loadPostProcess() also applies the same transformation to serialized m_levelUp/m_levelDown values at lines 4605–4607. That makes this more than a UI safeguard: it changes rank-progression state both during play and after loading. Maybe setSafeLevels() should be removed rather than adjusted. Following @xezon’s suggestion, terminal-rank detection can be derived from the current rank, rank count and applicable game rank limit. The shared UI calculation can then return zero when there is no attainable next rank or when the interval is nonpositive, without rewriting the underlying state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your analysis is correct, and it's a stronger argument than my original comment captured. The problem with The right approach is to fix the two ControlBar divisions defensively, without touching // In populatePurchaseScience and updateContextPurchaseScience
const Int interval = player->getSkillPointsLevelUp() - player->getSkillPointsLevelDown();
const Int progress = (interval > 0)
? ((player->getSkillPoints() - player->getSkillPointsLevelDown()) * 100) / interval
: 100;
GadgetProgressBarSetProgress(win, progress);That keeps the rank-progression state untouched, handles the zero-width interval gracefully (showing a full bar when the player is at or past the threshold), and doesn't require The |
||
| else | ||
| { | ||
| m_levelUp = levelUp; | ||
| m_levelDown = levelDown; | ||
| } | ||
| } | ||
|
|
||
| //============================================================================= | ||
| Bool Player::hasScience(ScienceType t) const | ||
| { | ||
|
|
@@ -4588,6 +4604,5 @@ void Player::xfer( Xfer *xfer ) | |
| // ------------------------------------------------------------------------------------------------ | ||
| void Player::loadPostProcess() | ||
| { | ||
|
|
||
| setSafeLevels(m_levelUp, m_levelDown); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.