Skip to content
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

Changing DelayTilMilli to used unsigned; patching "Wait 25 days" bug #122

Merged
merged 2 commits into from Aug 7, 2018

Conversation

jkoppel
Copy link
Owner

@jkoppel jkoppel commented Jul 22, 2018

I didn't find an easy way to test this (see https://stackoverflow.com/questions/727918/what-happens-when-gettickcount-wraps ), so I'm asking for manual inspection.

@jkoppel jkoppel requested review from mkristofik and Kert July 22, 2018 23:32
* in a very small number, it will never wait for very long.
*/
void __fastcall DelayTilMilli(long tick) {
unsigned long uTick = tick;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this const?

*/
void __fastcall DelayTilMilli(long tick) {
unsigned long uTick = tick;
while (UTickCount() < uTick)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about static_cast<unsigned long>(KBTickCount()) instead of a new function? It's ugly, but it makes the key bit (using a big enough unsigned type) obvious.


/*
* This is a slightly hacky fix to the bug described in https://www.celestialheavens.com/forum/7/16792 ,
* where a signed overflow of GetTickCount() can cause the game to wait for up to 25 days
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to add an upper bound to prevent the game ever hanging forever again? Is it ever reasonable for DelayTilMilli to wait for more than a few seconds?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were two problems that combined to form these long delays:

The first is the possibility of wraparound.

The second is that some code accidentally put in a very small number into DelayTillMilli.

The end result is waiting a very long time.

With this change, the only way DelayTillMilli could wait a very long time is if the caller passed in a very large number. In which case, either they really did want a very long wait, or it was a mistake, in which case we shouldn't hide it (though I guess we can detect it and exit with an error message). In any case, this change cannot introduce any new very-long-hangs where there were none before.

@jkoppel jkoppel merged commit 3f56e9a into master Aug 7, 2018
@jkoppel jkoppel deleted the jk-fix-wrap-tickcount branch July 14, 2019 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants