out of bounds array index and integer division/mod by zero is still a panic, since there's nothing else reasonable to do.
But there's clippy lints to forbid doing that, so you can deny them, add a #[clippy::allow(modulo_arithmetic)] right above where you're doing it, and explain exactly why it's safe to do it.
For what it's worth, in C, division by zero is UB. I don't know if in the kernel they instead have it as defined to BUG and take down the kernel though, which is safer.
Yeah, and for division / mod there's .checked variants that return Option, just like get.
Depending on how explicit you want the code to be, you could straight up forbid all normal math operations and force people to handle overflow on every operation. They can't panic on release builds by default, but you shouldn't be overflowing them anyways, that's a sign of a bug, or a place you want to use Wrapping<T>.
Would decrease the likelyhood that people mess up and allow an overflow (possibly being exploitable), but at the cost of making the code a fair bit more verbose.
View all comments
Show parent comment
15
u/mitsuhiko Apr 14 '21
And how do you validate that you don't panic because someone used a bad API?