Skip to content
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

Lint for returning a pointer to stack memory associated with a local variable #134215

Open
1c3t3a opened this issue Dec 12, 2024 · 0 comments · May be fixed by #134218
Open

Lint for returning a pointer to stack memory associated with a local variable #134215

1c3t3a opened this issue Dec 12, 2024 · 0 comments · May be fixed by #134218
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@1c3t3a
Copy link
Member

1c3t3a commented Dec 12, 2024

Code

pub fn foo() -> *const i32 {
    let x = 42;
    &x
}

Current output

rustc doesn't currently warn about this.

Desired output

A warning that this pointer is referencing a local variable and is going to be dangling immediately. E.g.

error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
  |
LL|  &x
  |  ^^

Rationale and extra context

This code is very unlikely to be correct, which is exactly why a warning should be emitted. This would be similar to Clang's -Wreturn-stack-address warning.

In case of returning a reference, rustc would catch this and emit a compiler error. While it is fine to create such a pointer, it is very unlikely that the code should be written that way.

Rust Version

rustc 1.84.0-nightly (b8c8287a2 2024-11-03)
binary: rustc
commit-hash: b8c8287a229cd79604aa84c25e1235fc78cd5f2e
commit-date: 2024-11-03
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.3

Anything else?

If this becomes a Warn lint it will start warning immediately and potentially break users that configured warnings as errors. So maybe this should be Allow for the beginning?

@1c3t3a 1c3t3a added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 12, 2024
Add a new lint that warns for pointers to stack memory

This adds a new lint with level `Warn` to check for code like:

```rust
fn foo() -> *const i32 {
  let x = 42;
  &x
}
```

and produce a warning like:
```text
error: returning a pointer to stack memory associated with a local variable
  --> <source>:12:5
   |
 LL|  &x
   |  ^^
```
This fixes rust-lang#134215.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant