Re:Regression in Linux 4.14 – using bcache can destroy the f...
by caf on Tuesday November 21, 2017 @05:57PM
(#15752426)
In this case the memory isn't uninitialised - it's been initialised by bio_init(), which sets bi_partno to 0.
Re:Regression in Linux 4.14 – using bcache can destroy the f...
by masklinn on Wednesday November 22, 2017 @05:39AM
(#15755815)
> it's been initialised by bio_init(), which sets bi_partno to 0.
It just memsets the entire thing to 0, which in Rust would
1. require unsafe{}
2. most likely be UB as the struct looks like it has a bunch of pointers which would not be nullable (e.g. arrays)
And therefore not be done at all, or rejected by reviewers if attempted.
Re:Regression in Linux 4.14 – using bcache can destroy the f...
by koverstreet on Tuesday November 21, 2017 @07:49PM
(#15753210)
You can return structs just fine in C. The reason it's not generally done in practice is because C lacks return value optimization - the compiler (almost?) always has to do a hidden memcpy() at the end of your function due to pointer aliasing.
Also, even if somebody did insist on explicit mut-output pattern in Rust anyway, they would quickly realize that they need to initialize the value before getting a mutable reference to it - Rust NEVER allows access to uninitialized memory (other than unsafe blocks where you can trick Rust into accesing uninitialized memory and cause undefined behavior by using `std::mem::uninitialized`). The noise related to it should hint that this is not the intended way to do it. Here is a code example to clarify what I mean.
And error message: Fixing that requires explicitly writing a value into `two`, for instance with `let mut two = 0`, which for complex structs... yeah, you may as well return a struct directly instead of bothering with pointer output.[std::mem::uninitialized]: https://doc.rust-lang.org/std/mem/fn.uninitialized.html