question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it undefined behavior to hold an invalid reference, if it is never dereferenced.

See original GitHub issue

It 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:closed
  • Created 7 years ago
  • Comments:45 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
arielb1commented, Jun 25, 2017

@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?

0reactions
RalfJungcommented, Jun 18, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found