What happens with integer overflow?

Place to get help with not working mods / modding interface.
Post Reply
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

What happens with integer overflow?

Post by FuryoftheStars »

I'm curious how this game/lua handles integer overflows. Specifically uint (but if it's different depending on the number type in question, knowing all could be good). Does it just roll over to the other end? Does it generate an error or undefined value?
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: What happens with integer overflow?

Post by DaveMcW »

All Lua 5.2 numbers are double precision floats. So it almost impossible to make them overflow.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens with integer overflow?

Post by FuryoftheStars »

Ok, then maybe it's the C++ side I'm interested in? Basically, I want to know what happens to a number like game.tick (which the docs show as a uint) when it overflows. Does it restart at 0, or does it generate some kind of undefined value and/or error out?
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: What happens with integer overflow?

Post by DaveMcW »

C++ integers wrap around to the minimum value. This does not affect the number itself, but any logic expecting the number to be large could fail/crash.

game.tick overflow is a popular source of bugs, some of which have been fixed. I'm not sure they are all fixed though.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens with integer overflow?

Post by FuryoftheStars »

Ok, thanks! In my case, with the mod I'm modifying, I'm taking game.tick and adding an additional number to it before storing it away. While this obviously shouldn't be an issue for a long time, I just wanted to make sure it wouldn't start causing crashing or anything when someone starts nearing the end of the map's game.tick. The fact that it wraps around is actually good and... I think... I shouldn't have to do anything special to catch.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

Xorimuth
Filter Inserter
Filter Inserter
Posts: 625
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

Re: What happens with integer overflow?

Post by Xorimuth »

FuryoftheStars wrote:
Thu May 05, 2022 3:03 pm
Ok, thanks! In my case, with the mod I'm modifying, I'm taking game.tick and adding an additional number to it before storing it away. While this obviously shouldn't be an issue for a long time, I just wanted to make sure it wouldn't start causing crashing or anything when someone starts nearing the end of the map's game.tick. The fact that it wraps around is actually good and... I think... I shouldn't have to do anything special to catch.
If the player is approaching the max value of game.tick then that's very much the player's problem (or wube's), not yours as a modder. See 101341 and viewtopic.php?f=18&t=101621 for more info.
My mods
Content: Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Remote Configuration | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens with integer overflow?

Post by FuryoftheStars »

Xorimuth wrote:
Thu May 05, 2022 3:21 pm
FuryoftheStars wrote:
Thu May 05, 2022 3:03 pm
Ok, thanks! In my case, with the mod I'm modifying, I'm taking game.tick and adding an additional number to it before storing it away. While this obviously shouldn't be an issue for a long time, I just wanted to make sure it wouldn't start causing crashing or anything when someone starts nearing the end of the map's game.tick. The fact that it wraps around is actually good and... I think... I shouldn't have to do anything special to catch.
If the player is approaching the max value of game.tick then that's very much the player's problem (or wube's), not yours as a modder. See 101341 and viewtopic.php?f=18&t=101621 for more info.
Yes, I'm fully aware. However, I don't want to contribute to the problem, plus, right in one of the threads you linked, the devs gave a "fix" to reset the counter so the map can continue being used: viewtopic.php?p=560436#p560436

I want to be forward thinking here and not have this mod be a reason why they cannot, or force them into uninstalling/reinstalling the mod.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: What happens with integer overflow?

Post by robot256 »

If you are doing math with game.tick and equality comparisons, you can account for the rollover. For example, "tick_to_wait_for = math.mod(game.tick+1000, 2^32)". If you are doing less than/greater than comparisons, then some extra logic will be required to handle the rollover, and possibly a memory of the "last tick" to trigger it.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens with integer overflow?

Post by FuryoftheStars »

Nah, I'm just doing a game.tick + factor, then using the result as the index in a table. Later, I recall data out of the table with the current game.tick (and once done processing what was stored there, I nil it). So if it rolls over to 0, that should be no issue in my use case.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: What happens with integer overflow?

Post by robot256 »

At tick 2^32-1, you calculate future_index=2^32-1+factor, which is a large positive number. It will never test equal to any subsequent game.tick values, because they roll over from 2^32-1 to 0, 1, 2 etc. So to make your code rollover proof you have to detect when future_index exceeds 2^32-1 and store the rolled-over value too. Otherwise you could have some stale entries for 2^32+N that never get deleted or acted on.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens with integer overflow?

Post by FuryoftheStars »

Wait... so can my math exceed 2^32? Shoot... I've been assuming that when I do math operations, it's still treating it as a 32-bit number. Now that I think about it, I guess I should've known better.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: What happens with integer overflow?

Post by robot256 »

Once it's in Lua, it stays as a double (52-bit integer capable) until you pass it back to another API function.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2541
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: What happens with integer overflow?

Post by FuryoftheStars »

Ok, so a test of “is result >= 2^32, and if so, subtract 2^32 from it” should work.

Thanks!
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

Post Reply

Return to “Modding help”