FAQ - Help and questions

What happens on integer overflow in Python, JavaScript, Java, C#, Go and Rust?

#porting#integers#overflowpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

The same addition of one to the largest 32-bit integer gives different results. Python grows the number, JavaScript uses a double, Java and Go wrap, C# wraps unless checked is used, and Rust panics in a debug build and wraps in release unless you choose checked, wrapping or saturating arithmetic.

Checked with rustc 1.93.0, Python 3.13.7, Node.js v22.19.0, Go 1.26.5, OpenJDK 17.0.18, .NET SDK 10.0.302, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Add 1 to 2147483647, the largest signed 32-bit integer. Each language was run on exactly that.

LanguageResultWhy
Python 3.13.72147483648Integers have no fixed size; masking with 0xFFFFFFFF is needed to get 32-bit behaviour
Node.js v22.19.02147483648; -2147483648 after a bitwise-or with 0Numbers are doubles; a bitwise operator forces a 32-bit integer
Java 17-2147483648; Math.addExact throws integer overflowint wraps; the exact methods check
C# (.NET 10.0.302)-2147483648 in unchecked; OverflowException in checkedThe default context here was unchecked
Go 1.26.5-2147483648Signed integers wrap
Rust 1.93.0debug build: panic; release build: -2147483648Overflow checks follow the build profile
Cundefined behaviour for signed intNot run here; this is what the C standard says

The Rust program shows all four of Rust's explicit choices, and the plain + last because it ends the program in a debug build. It uses black_box so that the compiler cannot compute the answer ahead of time:

src/main.rsrust
fn main() {
    let max = std::hint::black_box(i32::MAX);
    println!("wrapping_add  : {}", max.wrapping_add(1));
    println!("checked_add   : {:?}", max.checked_add(1));
    println!("saturating_add: {}", max.saturating_add(1));
    println!("plain +       : {}", max + 1);
}
text
(release build)
wrapping_add  : -2147483648
checked_add   : None
saturating_add: 2147483647
plain +       : -2147483648

(debug build)
wrapping_add  : -2147483648
checked_add   : None
saturating_add: 2147483647

thread 'main' panicked at src\bin\ovf2.rs:6:36:
attempt to add with overflow

The other languages, as run. Java:

java
int x = Integer.MAX_VALUE;
System.out.println(x + 1);
try { Math.addExact(x, 1); } catch (ArithmeticException e) { System.out.println(e.getMessage()); }
text
-2147483648
integer overflow

C#:

csharp
int x = int.MaxValue;
Console.WriteLine(unchecked(x + 1));
try { Console.WriteLine(checked(x + 1)); } catch (OverflowException e) { Console.WriteLine(e.Message); }
text
-2147483648
Arithmetic operation resulted in an overflow.

JavaScript printed 2147483648, then -2147483648 for (2147483647 + 1) | 0, and 9007199254740992 for 9007199254740992 + 1 (the doubles can no longer count by one). Python printed 2147483648 both for the plain sum and for the sum masked with 0xFFFFFFFF: the number is still below 2 to the power 32, so the mask leaves it alone, and Python never wraps by itself.

How to do it#

  1. For each integer in the old code, find out what it relied on: wrap-around (hashes, checksums, random number generators), growth (Python big integers), or "never overflows" in practice.
  2. For intended wrap-around, use wrapping_add, wrapping_mul and friends in Rust, or the Wrapping<T> type, so that the code means the same in debug and release.
  3. For arithmetic that must not overflow, use checked_* and handle None, or widen the type (i64, u128).
  4. For code ported from Python, choose a width from the data, or use a big-integer crate if values really have no bound (no crate was run here).
  5. Run the tests in both debug and release builds (cargo test and cargo test --release) so that both overflow behaviours are exercised.

Common mistakes#

  • Porting a hash function that relied on wrap-around with plain + and *. It panics in a debug build.
  • Using as to narrow a wider result and calling it a check (see the thread on C integers).
  • Believing that Rust always panics on overflow. The release default is to wrap.

Can I make release builds panic on overflow too?#

Yes, with overflow-checks = true in [profile.release]; it costs some speed. Measure before deciding.

Which type does a literal like 2147483647 have?#

Whatever the context needs; i32 if nothing else decides it.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet