Integer overflow

2,032pages on
this wiki
Add New Page
Add New Page Talk0

Integer overflow is what occurs when a numeric variable exceeds its maximum capacity. Generally what happens, due to two's complement arithmetic, is that the variable wraps to the opposite end of the spectrum. So for a signed variable, a negative value too large to fit would become a large positive value. An unsigned variable that exceeds its capacity would become a small positive value, or zero. Subtracting too much from a small unsigned variable would produce a large value.

A signed 8-bit variable (which is what AC uses) can store -128 to 127 inclusive. If your AC is -126 and you put on a piece of armor that would reduce your AC by five points, then your AC would become -131. -131 is too large for a signed 8-bit variable, so your new AC would be 124. 124 AC is of course extremely bad, considering a naked human has 10 AC. Thankfully, this doesn't happen in the case of AC.

Some integers in NetHack, like AC[1] and alignment[2], have protection against overflow. One way you can detect whether an overflow would occur is if subtracting from a negative value produces a positive value, or adding to a positive value produces a negative value. In the case of AC, the code calculates your current AC in a larger variable (varies, but most likely 32 bits), and then checks if the smaller AC variable (8 bits) would overflow.

Items with enchantments can go from -128 to +127 without overflowing.

It is also possible to cause integer overflow with containers or the player's inventory, causing them to have negative weight. 4294968 loadstones or 357914 boulders in a chest will cause an overflow to about -2147483647, or -(2^31). This tends to cause the Stressed/Burdened/Overloaded/Overtaxed status to go away. If inventory weight display is enabled, the player will see that the inventory weight is negative 2 billion and they will be able to pick up any object, provided that they have an inventory letter free. The player can also wish for objects such as boulders and not have them drop to the floor. Overflowing the player's inventory is only possible by wishing for cursed loadstones, and this usually can only be done in wizard mode. It is best to do this when the player is already carrying 52 different items so the loadstone will take up the '#' slot.

Source code referencesEdit

  1. Do_wear.c#find_ac
  2. Attrib.c#adjalign

Also on Fandom

Random Wiki