Skip to content

Add about overflow-checks flag in release mode #24

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

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/coding-guidelines/types-and-traits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ Types and Traits
:scope: module
:tags: numerics

Code must not rely on Rust's implicit integer wrapping behavior that occurs in release builds.
Instead, explicitly handle potential overflows using the standard library's checked,
Code must not rely on Rust's implicit integer wrapping behavior that may occur in release
builds. Instead, explicitly handle potential overflows using the standard library's checked,
saturating, or wrapping operations.

.. rationale::
:id: rat_kYiIiW8R2qD1
:status: draft

In debug builds, Rust performs runtime checks for integer overflow and will panic if detected.
However, in release builds (with optimizations enabled), integer operations silently wrap
around on overflow, creating potential for silent failures and security vulnerabilities.
However, in release builds (with optimizations enabled), unless the flag `overflow-checks`_ is
turned on, integer operations silently wrap around on overflow, creating potential for silent
failures and security vulnerabilities. Note that overflow-checks only brings the default panic
behavior from debug into release builds, avoiding potential silent wrap arounds. Nonetheless,
abrupt program termination is usually not suitable and, therefore, turning this flag on must
not be used as a substitute of explicit handling. Furthermore, the behavior in release mode is
under consideration by the The Rust Language Design Team and in the future overflow checking
may be turned on by default in release builds (it is a `frequently requested change`_).

.. _overflow-checks: https://github.com/rust-lang/rust/blob/master/src/doc/rustc/src/codegen-options/index.md#overflow-checks
.. _frequently requested change: https://lang-team.rust-lang.org/frequently-requested-changes.html#numeric-overflow-checking-should-be-on-by-default-even-in-release-mode

Safety-critical software requires consistent and predictable behavior across all build
configurations. Explicit handling of potential overflow conditions improves code clarity,
Expand Down