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

Do not suppress temporary_cstring_as_ptr in macros. #109944

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 8 additions & 41 deletions compiler/rustc_lint/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::lints::CStringPtr;
use crate::LateContext;
use crate::LateLintPass;
use crate::LintContext;
use rustc_hir::{Expr, ExprKind, PathSegment};
use rustc_hir::{Expr, ExprKind};
use rustc_middle::ty;
use rustc_span::{symbol::sym, ExpnKind, Span};
use rustc_span::{symbol::sym, Span};

declare_lint! {
/// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of
Expand Down Expand Up @@ -34,47 +34,14 @@ declare_lint! {

declare_lint_pass!(TemporaryCStringAsPtr => [TEMPORARY_CSTRING_AS_PTR]);

fn in_macro(span: Span) -> bool {
if span.from_expansion() {
!matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
} else {
false
}
}

fn first_method_call<'tcx>(
expr: &'tcx Expr<'tcx>,
) -> Option<(&'tcx PathSegment<'tcx>, &'tcx Expr<'tcx>)> {
if let ExprKind::MethodCall(path, receiver, args, ..) = &expr.kind {
if args.iter().any(|e| e.span.from_expansion()) || receiver.span.from_expansion() {
None
} else {
Some((path, *receiver))
}
} else {
None
}
}

impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if in_macro(expr.span) {
return;
}

match first_method_call(expr) {
Some((path, unwrap_arg)) if path.ident.name == sym::as_ptr => {
let as_ptr_span = path.ident.span;
match first_method_call(unwrap_arg) {
Some((path, receiver))
if path.ident.name == sym::unwrap || path.ident.name == sym::expect =>
{
lint_cstring_as_ptr(cx, as_ptr_span, receiver, unwrap_arg);
}
_ => return,
}
}
_ => return,
if let ExprKind::MethodCall(as_ptr_path, as_ptr_receiver, ..) = expr.kind
&& as_ptr_path.ident.name == sym::as_ptr
&& let ExprKind::MethodCall(unwrap_path, unwrap_receiver, ..) = as_ptr_receiver.kind
&& (unwrap_path.ident.name == sym::unwrap || unwrap_path.ident.name == sym::expect)
{
lint_cstring_as_ptr(cx, as_ptr_path.ident.span, unwrap_receiver, as_ptr_receiver);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/lint-temporary-cstring-as-ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@

use std::ffi::CString;

macro_rules! mymacro {
() => {
let s = CString::new("some text").unwrap().as_ptr();
//~^ ERROR getting the inner pointer of a temporary `CString`
}
}

fn main() {
let s = CString::new("some text").unwrap().as_ptr();
//~^ ERROR getting the inner pointer of a temporary `CString`
mymacro!();
}
19 changes: 17 additions & 2 deletions tests/ui/lint/lint-temporary-cstring-as-ptr.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: getting the inner pointer of a temporary `CString`
--> $DIR/lint-temporary-cstring-as-ptr.rs:7:48
--> $DIR/lint-temporary-cstring-as-ptr.rs:14:48
|
LL | let s = CString::new("some text").unwrap().as_ptr();
| ---------------------------------- ^^^^^^ this pointer will be invalid
Expand All @@ -14,5 +14,20 @@ note: the lint level is defined here
LL | #![deny(temporary_cstring_as_ptr)]
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
error: getting the inner pointer of a temporary `CString`
--> $DIR/lint-temporary-cstring-as-ptr.rs:8:52
|
LL | let s = CString::new("some text").unwrap().as_ptr();
| ---------------------------------- ^^^^^^ this pointer will be invalid
| |
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
...
LL | mymacro!();
| ---------- in this macro invocation
|
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
= note: this error originates in the macro `mymacro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors