-
Notifications
You must be signed in to change notification settings - Fork 5
UB + couple of mistakes in the string post #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for providing those typo fixes. I'll apply them to the source for the article. Miri thinks this 0 is "UB" for stupid reasons that boil down to materializing a pub fn owned(mut data: Box<str>) -> Self {
let len = data.len();
let ptr = data.as_mut_ptr();
mem::forget(data);
Self {
raw: RawYarn::from_raw_parts(HEAP, len, ptr),
_ph: PhantomData,
}
} Incidentally, this is a problem unique to Box and the compiler's special knowledge of it (primarily a historical accident). Arguably, Miri is incorrect for flagging this, since it works fine for Vec: fn main() {
// Works fine (but triggers miri's leaksan, which is not UB).
let mut data = vec![1];
let ptr = data.as_ptr().cast_mut();
std::mem::forget(data);
unsafe { &mut *ptr; }
// Spurious tagging error.
let mut data = vec![1].into_boxed_slice();
let ptr = data.as_ptr().cast_mut();
std::mem::forget(data);
unsafe { &mut *ptr; } // Tagging error occurs here.
} This is the sort of weird gotcha that shouldn't be UB, particularly since it's not justified by corresponding potential compiler optimizations. |
Stop lying please. See also: rust-lang/unsafe-code-guidelines#326 |
While I disagree with the tone of the previous message, it’s correct in its factual part: move of the |
What the hell are you talking about? Please take this toxic line of discussion elsewhere instead of trying to mansplain how LLVM works to me. |
Hi! I enjoyed reading your latest post about yarn, so I decided to play around with it a little. There’re a couple problems with code as present in the post (I’m not sure about the code in the actual library).
The main problem is that this function:
contains UB. Calling
mem::forget(data);
asserts unique ownership ofdata
and invalidatesptr
. That’s a common pitfall when usingmem::forget()
. The right way to do it is this:I’ve also noticed (and fixed) a couple of typos and minor mistakes that prevented the code from compiling. Here’s the playground, with all the relevant changes marked with
(!)
for easy search: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bb572789e965be9ce194889684bf2afa. You can run it under Miri to check that UB is present in the first version and absent in the second.I’d submit a PR, but I’m not sure where the sources for the posts are.
The text was updated successfully, but these errors were encountered: