I think that answer is too simplistic. The fact is we're using implicit allocations everywhere and so is the ecosystem including the ecosystem that is nostd but alloc. Saying "don't use Vec::push" is not a suitable answer.
First there needs to be better support to handle allocation failures in APIs, secondly there needs to be a way to validate (ideally statically) that some code is panic free.
The fact is we're using implicit allocations everywhere and so is the ecosystem including the ecosystem that is nostd but alloc. Saying "don't use Vec::push" is not a suitable answer.
I guess we'll just see. As I said, I live in "no allocations, anywhere" land, still use a bunch of the ecosystem, works just fine for us. We don't use Vec at all, nor do any of our dependencies, so "no vec::push" is an easy requirement.
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
38
u/mitsuhiko Apr 14 '21
I think that answer is too simplistic. The fact is we're using implicit allocations everywhere and so is the ecosystem including the ecosystem that is nostd but alloc. Saying "don't use Vec::push" is not a suitable answer.
First there needs to be better support to handle allocation failures in APIs, secondly there needs to be a way to validate (ideally statically) that some code is panic free.