Skip to contentSkip to conversation

rust: `overflow_assert!` macro

PATCH Superseded Antonio Hickey 1 participant 1 message Aug 19, 2025 Rust

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover rust: `overflow_assert!` macro

This patch set introduces a macro for overflow assertions, the use of this macro will avoid local #ifdefs by encapsulating the conditional behavior to the macro. In addition this macro also allows us to document the intent of the assertion more clearly.

Changes in v5:
- Refactored the overflow_assert! macro to forward all tokens
to the assert!, allowing full message formatting and parity
with debug_assert!.
- Improved the documentation of the overflow_assert! macro.
- Updated example use patch to also use a custom message.
- Link to v4: https://lore.kernel.org/all/[email protected]/

Changes in v4:
- Added the ability for callers to supply a custom message to the
overflow assertion.
- Small change to verbage in documentation.
- Link to v3: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- Made the overflow_assert! macro take in expressions instead of
values.
- Fixed path in macro to fully qualify.
- Refactor macro to make the conditional compilation as local as
possible.
- Refactor macro to utilize the cfg! macro like the standard library
does to reduce boilerplate.
- Added an example use case of the overflow_assert! macro
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/

Changes in v2:
- Fixed example doc comment so it doesn't panic.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

Antonio Hickey (2):
rust: kernel: create overflow_assert! macro
rust: uaccess: refactor to use overflow_assert!

 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 44 ++++++++++++++++++++++++++++++++++
 rust/kernel/uaccess.rs         |  4 ++--
 3 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 rust/kernel/overflow_assert.rs
Signature · 1 line
2.50.1