3 ms·
You can catch the panic. It will panic, but I don't know if the driver will catch it. I hope so :)
by EugeneOZ 4y ago
You can catch the panic. It will panic, but I don't know if the driver will catch it. I hope so :)
- ncmncm 4y agoThis not actually substantively different from throwing an exception.
- EugeneOZ 4y agoIndeed, but it's much better than undefined behavior :)
- davidatbu 4y agoI believe Rust in linux was made so that it never panics. Here's a patch that removes panicking allocations for example: https://lore.kernel.org/lkml/20210704202756.29107-1-ojeda@kernel.org/ https://lore.kernel.org/lkml/20210704202756.29107-1-ojeda@ke... (but I think all other instances of panicking were removed as well). EDIT: Look at replies. "Linus considers panics acceptable in some cases".
- ncmncm 4y agoI presume it will still Oops...
- tialaramex 4y agoLinus considers it acceptable to panic for some programming mistakes, since after all the C code also blows up if you make some programming mistakes. One I ran into (in the sense of read about, not experienced) was if I flatten a Vec of arrays, it's theoretically possible that the flattened structure has too many items in it to represent as a machine word integer. If this happens the flatten operation will panic. This can't happen in a language like C (or C++) because their smallest type has size 1, so all the arrays can't possibly be bigger in total size than the amount of memory, that's nonsense. But Rust has two smaller sizes than this. The relevant one here is the Zero Size Type, Rust has no problem with the idea of an array of empty tuples, such an array could have say, a billion empty tuples in it, yet on a 32-bit system it just needs 4 bytes (to remember how many empty tuples are in it). We can see that flattening a Vec of arrays of empty tuples is a pretty wild thing to choose to do, and nevertheless even if we do it, it only panics when the total amount of empty tuples won't fit in the integers of our native word size. But the function could be asked to do this, and so it might panic. [ You might be wondering how can there be two sizes smaller than C's single byte types in Rust. The answer is the Never type ! and its pseudonym Infallible. The Never type is Empty, no values of this type are possible, so not only does Rust never need to store this type, it doesn't even need to emit machine code to handle this type - the code could never run. This makes sense in Generic programming, we can write Generic error handling code, but it evaporates when the error type was Infallible ]
- davidatbu 4y agoThis is exactly the kind of stuff I come on HN for. Thank you!
- kzrdude 4y agoCan you, in kernel context?
- EugeneOZ 4y agoAfter comments below, I'm not so sure. I was talking about regular Rust, I didn't know that Linux Rust is patched. Sorry.