Is it undefined behavior to hold an invalid reference, if it is never dereferenced.
See original GitHub issueIt is not Safe in rust code to create invalid references, in that these references can be used by safe code to trigger Undefined Behavior, however, is it Undefined Behavior to create one of these references?
For example, does this code trigger Undefined Behavior?
struct Foo {...}
let _x: &const Foo = &*(1024 as *const Foo);
How about this code, which gets the address of the member, but does no reading?
struct Foo {
member: i32
}
let _x: &i32 = &(*(1024 as *const Foo)).member;
How about this code, which creates an invalid reference by over-extending the lifetime, but does not follow it?
struct Foo {...}
let _x: &'static Foo;
{
let y = Foo {...};
_x = mem::transmute(&y);
}
Or this code, which creates a reference with the wrong lifetime, but only follows it while the object behind it is still alive?
struct Foo {
member: i32
}
let y: &'static Foo;
{
let x = Foo {...};
y = mem::transmute(&x);
println!("y.member = {}", y.member);
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:45 (9 by maintainers)
Top Results From Across the Web
Is it undefined behavior in C++ to dereference an invalid ...
According to the standard, dereferencing a non-initialised pointer is undefined behaviour. However, in real life this is ...
Read more >What Unsafe Can Do - The Rustonomicon
Invoking Undefined Behavior gives the compiler full rights to do arbitrarily bad ... a wide reference, Box , or raw pointer that has...
Read more >EXP34-C. Do not dereference null pointers
A non-null but invalid pointer passed to memcpy() can indeed cause undefined behavior, but that is not the issue in the noncompliant code...the...
Read more >Undefined behavior - Wikipedia
In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language ......
Read more >P2414R1 Pointer lifetime-end zap proposed solutions
Invalid pointer: A pointer referencing an object whose storage duration has ... Access invalid pointers, but never dereference them. ... undefined behavior.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ubsan
First, at least for
nonnull
/range
that’s how LLVM works, so we don’t have much of a choice.Also, what’s the problem with that idea?
This is now becoming the validity invariant discussion at https://github.com/rust-lang/unsafe-code-guidelines/issues/76 and https://github.com/rust-lang/unsafe-code-guidelines/issues/77.