Skip to content

Missing build warning "temporary_string_as_ptr" when String is used with "as_ptr" on one line #90449

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

Closed
mirao opened this issue Oct 31, 2021 · 2 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@mirao
Copy link

mirao commented Oct 31, 2021

Playground

It's rather a suggestion than a bug.

Have this code:

use std::ffi::CString;

fn main() {
    let pointer = CString::new("Hello world!").unwrap().as_ptr();
    unsafe {
        println!("{:?}", *pointer as u8 as char);
    }
}

Rust reports the warning rustc(temporary_cstring_as_ptr) during build. The detail is this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime which is expected 🆗 .
If you run the code, a "random" character will be printed and that's of course unexpected. A solution is an assignment of the string into a standalone variable as explained here.


Have the same code, but this time it uses String instead of CString:

fn main() {
    let pointer = String::from("Hello world!").as_ptr();

    unsafe {
        println!("{:?}", *pointer as char);
    }
}

I expected to see this happen:

  • I should get similar kind of warning as in case of CString, because also in this case the pointer is dangling and if you run the code, a random character will be printed. A solution is the same as for CString

Instead, this happened:

  • Rust doesn't warn

Meta

rustc --version --verbose:

rustc 1.58.0-nightly (e249ce6b2 2021-10-30)
binary: rustc
commit-hash: e249ce6b2345587d6e11052779c86adbad626dff
commit-date: 2021-10-30
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
@mirao mirao added the C-bug Category: This is a bug. label Oct 31, 2021
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. needs-triage-legacy labels Jan 24, 2024
@fmease
Copy link
Member

fmease commented Jan 24, 2024

Note that miri catches this with:

error: Undefined Behavior: memory access failed: alloc859 has been freed, so this pointer is dangling
 --> src/main.rs:5:26
  |
5 |         println!("{:?}", *pointer as char);
  |                          ^^^^^^^^ memory access failed: alloc859 has been freed, so this pointer is dangling
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: alloc859 was allocated here:
 --> src/main.rs:2:19
  |
2 |     let pointer = String::from("Hello world!").as_ptr();
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: alloc859 was deallocated here:
 --> src/main.rs:2:56
  |
2 |     let pointer = String::from("Hello world!").as_ptr();
  |                                                        ^
  = note: BACKTRACE (of the first span):
  = note: inside `main` at src/main.rs:5:26: 5:34

@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 24, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
@jieyouxu
Copy link
Member

jieyouxu commented May 1, 2025

Triage: closing in favor of a newer issue since temporary_string_as_ptr was renamed and generalized into dangling_pointers_from_temporaries in #128985.

@jieyouxu jieyouxu closed this as completed May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants