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 suggestion for Cow::to_ptr #6349

Closed
mulimoen opened this issue Nov 19, 2020 · 1 comment
Closed

Lint suggestion for Cow::to_ptr #6349

mulimoen opened this issue Nov 19, 2020 · 1 comment
Labels
A-lint Area: New lints

Comments

@mulimoen
Copy link

mulimoen commented Nov 19, 2020

What it does

Checks for temporary Cow immediately dereferenced

Categories (optional)

  • Kind: clippy::correctness

What is the advantage of the recommended code over the original code

The original code will produce a dangling pointer on a Cow::Owned if created as a function argument. This is the same situation as for CString which will lead to a use-after-free.

Drawbacks

None.

Example

use std::borrow::Cow;

fn take_ptr(_p: *const i32) { }

fn main() {
    take_ptr(Cow::<[i32]>::Owned(vec![0]).as_ptr());
    take_ptr(Cow::<[i32]>::Borrowed(&[1]).as_ptr());
}

Could be written as:

use std::borrow::Cow;

fn take_ptr(_p: *const i32) { }

fn main() {
    let c = Cow::<[i32]>::Owned(vec![0]);
    take_ptr(c.as_ptr());
    let c = Cow::<[i32]>::Borrowed(&[1]);
    take_ptr(c.as_ptr());
}

Edit: I see that

     take_ptr(vec![2])

also does not have a lint and exposes the same problem

@mulimoen
Copy link
Author

Duplicate of #2637 and #5965

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant