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

Undocumented panics in std::process on unix for strings with interior nuls #30858

Closed
kamalmarhubi opened this issue Jan 12, 2016 · 7 comments
Closed
Labels
P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@kamalmarhubi
Copy link
Contributor

All the strings are taken as AsRef<OsStr> and converted to CString with unwrapping: https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/process.rs#L83-L85

An example for Command::new (playground):

use std::process::Command;

fn main() {
    Command::new("\0");
}

The panic should be documented, or the conversion unwrap should be delayed until a call to one of the methods that start the process so that the error can be returned in a Result.

Version:

$ rustc --version --verbose
rustc 1.5.0 (3d7cd77e4 2015-12-04)
binary: rustc
commit-hash: 3d7cd77e442ce34eaac8a176ae8be17669498ebc
commit-date: 2015-12-04
host: x86_64-unknown-linux-gnu
release: 1.5.0
@talchas
Copy link

talchas commented Jan 12, 2016

This also occurs with arg(), but env() in fact doesn't panic /at all/ for \0 (just silently gives you an environ you probably don't intend), because of accidents of implementation, which seems very sketchy.

@kamalmarhubi
Copy link
Contributor Author

Given that it's possible to delay the conversion until spawning, I think that is the leas surprising thing to do. The env vars can be checked at that point as well.

@kamalmarhubi
Copy link
Contributor Author

That would also be similar to what happens if you attempt to create a file (playground):

use std::fs::File;

fn main() {
    println!("{:?}", File::create("/tmp/\0"));
}

prints

Err(Error { repr: Custom(Custom { kind: InvalidInput, error: StringError("path contained a null") }) })

@kamalmarhubi
Copy link
Contributor Author

@talchas

This also occurs with arg(), but env() in fact doesn't panic /at all/ for \0 (just silently gives you an environ you probably don't intend), because of accidents of implementation, which seems very sketchy.

I decided the behaviour with env() is different enough to warrant its own bug. It isn't very good: http://is.gd/swL9IL

@alexcrichton
Copy link
Member

triage: I-nominated

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jan 14, 2016
@kamalmarhubi
Copy link
Contributor Author

I've got a patch in the works for this.

@alexcrichton
Copy link
Member

triage: P-medium

The libs team discussed this in triage yesterday and the conclusion was that the defer-the-error strategy is fine for Command. We discussed briefly that this may be altering the contract of Command (e.g. changing panics to results), but the Result behavior seems more consistent with the std::fs APIs.

@rust-highfive rust-highfive added P-medium Medium priority and removed I-nominated labels Jan 21, 2016
kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Feb 3, 2016
This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes rust-lang#30858
Fixes rust-lang#30862
bors added a commit that referenced this issue Feb 3, 2016
…hton

This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes #30858
Fixes #30862
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants