-
Notifications
You must be signed in to change notification settings - Fork 59
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
Validity invariant for containers with unitialized data #393
Comments
This code is not intended to be (language-level) UB. It violates safety requirements but not validity requirements. This means that calling other safe Vec operations later can cause UB (e.g. Also see this blog post on validity vs safety invariants. |
@RalfJung thanks for the reply! Oh, okay, I was interpreting But what you're saying is that I can have memory for a type let mut v: Vec<i32> = Vec::with_capacity(1);
unsafe { v.set_len(1) }
v[0] = 1i32; edit: by the way, I read your blog a few times already, great stuff! What confused me was that I assumed that the validity rules applied to the type |
let mut v: Vec<i32> = Vec::with_capacity(1);
unsafe { v.set_len(1) }
v[0] = 1i32; The third line here adds an additional question, as it creates an |
@deltragon thanks a lot for the link, that clears up a lot! I'm closing the issue since my question was answered. |
@hackaugusto Note also that it is very easy to run miri in the playground (it's under tools in the top right): snippet |
Also remember that all the statements about which of these has UB is relative to the current Vec internal implementation. That implementation can change in the future which can lead to more UB. Consult the docs to learn which of these aspects are actually a stable promise.
|
This is the same link as the other link in your reply, and |
I hope this is a good channel to ask this, let me know if I should use something else :)
I was trying to grasp what is considered UB w.r.t. uninitialized data. My current understanding is that there are validity rules, and that by definition uninitialized data is not valid for any type except
union
andpadding
.From what I understood from the reference UB happens when the value is produced, and it describes it as:
It seems there is an issue with that definition for code like this:
IIUC that second line above should be UB, but the rule of producing doesn't seem to cover that, since there is no data being assigned or read.
My questions are: 1. is it correct to say the above is undefined behavior? 2. am I missing some other rules that would define the above as UB?
Thanks!
The text was updated successfully, but these errors were encountered: