You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
saw_nul is never reset for std::process::Command on Linux #165316
A-processArea: `std::process` (note: `std::env` is A-env)C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.
Parameters for std::proces::Command, such as the program name, args, current_dir, and env variables (the list is not exhaustive), set saw_nul flag when the value contains a null byte. However, this flag is not cleared even when the offending value is cleared, leaving the command in unusable state.
use std::process::Command;fnmain(){letmut cmd = Command::new("/bin/true");println!("{:?}", cmd.status());
cmd.env("BAD\0","");println!("{:?}", cmd.status());
cmd.env_clear();// Doesn't work even after clearing the envprintln!("{:?}", cmd.status());}
Output:
Ok(ExitStatus(unix_wait_status(0)))
Err(Error { kind: InvalidInput, message: "nul byte found in provided data" })
Err(Error { kind: InvalidInput, message: "nul byte found in provided data" })
We should be able to solve this fairly cheaply by checking the strings again only if saw_nul is set. Since this is going to be the cold case, it shouldn't affect the perf of the type for regular use.
Could do a fix for this, been tinkering around with the unix Command stuff for a different bug (that qwaz pointed out in a separate issue a while back ironically) in #157144. This particular issue qwaz is pointing out seems to be occurring here:
From qwaz's demo, the issue could also occur if we called Command::exec after the Command::env_clear since it has the same if self.saw_nul() { ... } (ngl, I'm really not fond of the saw_nul flag which is only seen in the unix Command inner struct, and it tries to encapsulate if we saw an interior nul byte in arg, env, program name, etc., which makes it hard to differentiate if the saw_nul flag was set to true due to arg, env, program name, etcs and we can't just mark it false on Command::env_clear as a result).
We should be able to solve this fairly cheaply by checking the strings again only if saw_nul is set.
I don't think I'm following with what you're suggesting. However, since the only place we set saw_nul to true when we look into env variables is in Command::capture_env + construct_envp:
letmut result = CStringArray::with_capacity(env.len());
for(mut k, v)in env {
// Reserve additional space for '=' and null terminator
k.reserve_exact(v.len() + 2);
k.push("=");
k.push(&v);
// Add the new entry into the array
ifletOk(item) = CString::new(k.into_vec()){
result.push(item);
}else{
*saw_nul = true;
}
}
result
}
I think the easier and cleaner thing to do is to have each of those function propagate a result, i.e. io::Result<Option<CStringArray>>/io::Result<CStringArray>, where we'd just return the same error that the saw_nul check does below. We probably want to deviate any shared reliance on saw_nul flag for the env variables because we are able to clear/remove our environment variables.
On the plus side, we can exit early in construct_envp instead of just going through the rest of the BTreeMap<OsString, OsString>.
A-processArea: `std::process` (note: `std::env` is A-env)C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.
Activity
maxdexh commentedon Nov 2, 2026
We should be able to solve this fairly cheaply by checking the strings again only if
saw_nulis set. Since this is going to be the cold case, it shouldn't affect the perf of the type for regular use.asder8215 commentedon Nov 4, 2026
Could do a fix for this, been tinkering around with the unix
Commandstuff for a different bug (that qwaz pointed out in a separate issue a while back ironically) in #157144. This particular issue qwaz is pointing out seems to be occurring here:rust/library/std/src/sys/process/unix/unix.rs
Lines 62 to 69 in 708c94e
From qwaz's demo, the issue could also occur if we called
Command::execafter theCommand::env_clearsince it has the sameif self.saw_nul() { ... }(ngl, I'm really not fond of thesaw_nulflag which is only seen in the unixCommandinner struct, and it tries to encapsulate if we saw an interior nul byte in arg, env, program name, etc., which makes it hard to differentiate if thesaw_nulflag was set to true due to arg, env, program name, etcs and we can't just mark it false onCommand::env_clearas a result).I don't think I'm following with what you're suggesting. However, since the only place we set
saw_nulto true when we look into env variables is inCommand::capture_env+construct_envp:rust/library/std/src/sys/process/unix/common.rs
Lines 343 to 346 in 708c94e
rust/library/std/src/sys/process/unix/common.rs
Lines 384 to 401 in 708c94e
I think the easier and cleaner thing to do is to have each of those function propagate a result, i.e.
io::Result<Option<CStringArray>>/io::Result<CStringArray>, where we'd just return the same error that thesaw_nulcheck does below. We probably want to deviate any shared reliance onsaw_nulflag for the env variables because we are able to clear/remove our environment variables.On the plus side, we can exit early in
construct_envpinstead of just going through the rest of theBTreeMap<OsString, OsString>.@rustbot claim