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

CString's as_ptr is easy to get very wrong #16630

Closed
spaolacci opened this issue Aug 20, 2014 · 1 comment
Closed

CString's as_ptr is easy to get very wrong #16630

spaolacci opened this issue Aug 20, 2014 · 1 comment

Comments

@spaolacci
Copy link
Contributor

Whilst the documentation explicitly mentions the situation, precise scope is easy to get wrong.

// .as_ptr returns an internal pointer into the CString, and may be invalidated when the                                                                                        
// CString falls out of scope (the destructor will run, freeing the allocation if there is                                                                                      
// one).                                                                                                                                                                        
let s = "foo";                                                                                                                                                                  
let s1 = s.to_c_str(); // Ok.                                                                                                                                          
let s2 = s.to_c_str().as_ptr(); // Oops.                                                                                                                                        
//         ^~~~ tmp CString falls out of scope.                                                                                                                                 

let (c1, c2) = unsafe{ (*s1.as_ptr(), *s2) };                                                                                                                                   
println!("{}, {}", c1, c2); //  Prints `102, 0'

The same pattern is even easier to overlook in e.g libc::open(filename.to_c_str().as_ptr(), ...).

String's with_c_str should probably be preferred (or documented as preferred).

@aturon
Copy link
Member

aturon commented Aug 20, 2014

Dupe of #16035

@aturon aturon closed this as completed Aug 20, 2014
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 3, 2024
…ce, r=Veykril

fix: Wrong closure kind deduction for closures with predicates

Completes rust-lang#16472, fixes rust-lang#16421

The changed closure kind deduction is mostly simlar to `rustc_hir_typeck/src/closure.rs`.
Porting closure sig deduction from it seems possible too and I'm considering doing it with another PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants