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

Consider returning &c_char from CString::as_ptr #16035

Closed
Ms2ger opened this issue Jul 27, 2014 · 16 comments
Closed

Consider returning &c_char from CString::as_ptr #16035

Ms2ger opened this issue Jul 27, 2014 · 16 comments
Assignees
Labels
I-needs-decision Issue: In need of a decision.
Milestone

Comments

@Ms2ger
Copy link
Contributor

Ms2ger commented Jul 27, 2014

This should make it a lot harder to run into the bug mentioned in its documentation (let p = foo.to_c_str().as_ptr();).

@pcwalton
Copy link
Contributor

+1

@metajack
Copy link
Contributor

We hit this a ton in Servo in this latest rust upgrade, which caused many use after free bugs in otherwise safe code.

I think as_ptr() should be removed, and replaced with as_ref(), so the lifetime annotations stick and prevent this easy-to-make error.

@pcwalton
Copy link
Contributor

cc @aturon

@pcwalton
Copy link
Contributor

nominated for 1.0 P-backcompat-libs

@aturon
Copy link
Member

aturon commented Jul 30, 2014

The return type should be &[c_char] rather than &c_char, I'm assuming?

Just to clarify, the as_bytes and as_bytes_no_nul methods already provide this functionality.

I'd like to understand better how CString is being used in Servo. Within libstd, the predominant use is cases where you need to get a *const i8 to pass along to some C bindings. (When consuming C strings, we generally use from_c_str, which has recently been renamed to from_buf.)

I could imagine trying to do something more RAII-ish here, and return an object that derefs into *const i8, which would probably help catch some of these bugs.

Thoughts?

cc @alexcrichton

@alexcrichton
Copy link
Member

I believe the idea is that &c_char will implicitly coerce to *const c_char whereas &[c_char] will require another method call, sort of the difference between

foo(s.as_ref())
// vs
foo(s.as_bytes().as_ptr())

I'd be ok moving this over to &c_char, although I'd like to see the impact first to make sure it doesn't turn up too many surprises.

@pnkfelix
Copy link
Member

P-backcompat-libs, but not 1.0: this is in part of the stdlib that is still tagged with experimental stability attribute.

@aturon
Copy link
Member

aturon commented Jan 8, 2015

@alexcrichton Should we repurpose this bug with the new CString API?

@alexcrichton
Copy link
Member

This may be less relevant now that the API of CString has been pared back so much. It's much more clear that CString::from_slice(foo).as_ptr() is probably not going to outlive the expression thatn foo.to_c_str().as_ptr(), so I'd also just be fine closing this.

@Ms2ger
Copy link
Contributor Author

Ms2ger commented Jan 12, 2015

From IRC today:
<noodle> Why are these two c-pointers the same? http://is.gd/2KlvtT

use std::ffi::CString;

fn main() {
    let a = "1";
    let b = "2";
    let a_ptr = CString::from_slice(a.as_bytes()).as_ptr();
    let b_ptr = CString::from_slice(b.as_bytes()).as_ptr();

    println!("{:?}", a_ptr);
    println!("{:?}", b_ptr);
}

@aturon
Copy link
Member

aturon commented Jan 13, 2015

@alexcrichton I'm not sure I agree, it seems like exactly the same pitfalls are there. I think we should consider adding a method like the one proposed in this issue.

@aturon
Copy link
Member

aturon commented Feb 16, 2015

Nominating for 1.0-beta (P-backcompat-libs, as it already is).

@pnkfelix
Copy link
Member

1.0 beta, P-backcompat-libs, I-needs-decision,.

@pnkfelix pnkfelix added I-needs-decision Issue: In need of a decision. and removed I-nominated labels Feb 19, 2015
@pnkfelix pnkfelix added this to the 1.0 beta milestone Feb 19, 2015
@aturon
Copy link
Member

aturon commented Feb 21, 2015

Decision: we want to make a change like this, but there are some other ramifications for using CString this way. In particular there are patterns around Option<CString> for representing nullable data and converting to a pointer that this would make very painful.

We want to introduce some general libraries to cover patterns like that before we make this change.

So all told, we're OK stabilzing as_ptr for 1.0, and then later deprecating in favor of as_ref when this other infrastructure is ready.

Closing as WONTFIX (for now).

@aturon aturon closed this as completed Feb 21, 2015
@alexcrichton
Copy link
Member

I have opened an RFC issue about this issue more broadly which may help alleviate some concerns here as well.

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 11, 2023
fix: Fix diagnostics panicking when resolving to different files due to macros

Fixes rust-lang/rust-analyzer#14968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-needs-decision Issue: In need of a decision.
Projects
None yet
Development

No branches or pull requests

6 participants