Signed Integer Overflow UB

This cluster centers on signed integer overflow being undefined behavior (UB) in C/C++, allowing compilers to optimize away overflow checks and assume no overflow occurs, with debates on security risks, alternatives like wrapping or trapping, and comparisons to Rust's debug panics and release wrapping.

πŸ“‰ Falling 0.3x Programming Languages
3,337
Comments
19
Years Active
5
Top Authors
#1256
Topic ID

Activity Over Time

2008
2
2009
5
2010
16
2011
37
2012
26
2013
63
2014
277
2015
156
2016
296
2017
146
2018
249
2019
128
2020
260
2021
333
2022
505
2023
361
2024
217
2025
242
2026
18

Keywords

e.g UB IMO JS BigInt FixedInt WANT godbolt.org Cargo.toml i.e overflow integer undefined ub compiler checks signed behaviour unsigned wrapping

Sample Comments

bluGill β€’ Oct 13, 2025 β€’ View on HN

In some languages overflow is asserted as a can't happen and so the optimizer will remove your checks

nitrogen β€’ Apr 29, 2014 β€’ View on HN

Signed integer overflow is undefined in C, so optimizers can do really weird things to code that has overflows.

MindSpunk β€’ Oct 13, 2025 β€’ View on HN

Signed overflow is UB in C/C++ and several compilers will skip explicit overflow checks as a result. See: https://godbolt.org/z/WehcWj3G5

marin049 β€’ Oct 15, 2024 β€’ View on HN

Integer overflow is actually undefined behaviour thus the compiler is free to assume it doesn't happen.

quelsolaar β€’ Sep 13, 2020 β€’ View on HN

That's correct! C doesn't require checking for overflows, but it also doesn't forbid implementations from doing so. both are features.

masklinn β€’ Nov 2, 2020 β€’ View on HN

By default yes, but you can enable overflow checking in release mode (it’s a conf / compiler flag), and it has standard functions for checked, wrapping, and saturating ops.

kevincox β€’ Dec 11, 2022 β€’ View on HN

When I hear about making overflow safe by making it wrap my main question is how much code is actually ready for overflow? I've seen many vulnerabilities where a bounds check was bypassed because a computation overflowed. It seems that the only really safe behaviour here is crashing. If you aren't going to pay the cost of these checks may as well call it undefined behaviour and allow optimizations. (of course also including wrapping integer types in your language for when you do want t

sesuximo β€’ Jun 27, 2021 β€’ View on HN

While not standardized, there are compiler builtins for operations which care about overflow. They will not be optimized out and are very efficient (cost ~1 cycle if no overflow).

FreeFull β€’ Oct 26, 2014 β€’ View on HN

Unsigned overflow is defined, signed overflow isn't.

kwhitefoot β€’ Apr 17, 2020 β€’ View on HN

Strong type checking should be able to detect this kind of overflow statically. Probably not practical in the kinds of software involved though.