Skip to content

Commit f9ba5b7

Browse files
committed
[WIP] size limit for command line
windows/process: fix some obvious issues, create some more It looks like I really can't get away with the whole storing error thing as they don't have Copy. Maybe I will end up using the ugly problem enum thing like I did with Unix too. Grrr... unix: incremental size, no strlen windows: sort the error type out unix, windows: add temporary dead_code unix: cache envp Windows: draft traits for raw args and custom escapers Windows: raw args trait Windows: minor rename while I figure out where to put sized unix: minor error stuff windows: very rough xargs minor: run formatter it wants tight lines! Windows commandext: apply review suggestions Attempt at code reuse Windows builds. Unix not so much. It builds! Uh. fixup! re-enable set_errno on linux cleanup after slighty botched rebase remove a redundant macro arg appease tidy process_common: subtract page size for headroom
1 parent e84a8ca commit f9ba5b7

27 files changed

+606
-108
lines changed

Cargo.lock

+10-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ dependencies = [
263263
"cargo-util",
264264
"clap",
265265
"crates-io",
266+
<<<<<<< HEAD
266267
"crossbeam-utils 0.8.3",
268+
||||||| parent of 1725fb7e1bd (actually get size adjustment to work)
269+
"crossbeam-utils 0.8.0",
270+
=======
271+
"crossbeam-utils 0.8.0",
272+
"crypto-hash",
273+
>>>>>>> 1725fb7e1bd (actually get size adjustment to work)
267274
"curl",
268275
"curl-sys",
269276
"env_logger 0.8.1",
@@ -5569,11 +5576,12 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
55695576

55705577
[[package]]
55715578
name = "vergen"
5572-
version = "5.1.0"
5579+
version = "5.1.5"
55735580
source = "registry+https://github.com/rust-lang/crates.io-index"
5574-
checksum = "dfbc87f9a7a9d61b15d51d1d3547284f67b6b4f1494ce3fc5814c101f35a5183"
5581+
checksum = "adf0b57f76a4f7e9673db1e7ffa4541d215ae8336fee45f5c1378bdeb22a0314"
55755582
dependencies = [
55765583
"anyhow",
5584+
"cfg-if 1.0.0",
55775585
"chrono",
55785586
"enum-iterator",
55795587
"getset",

library/std/src/os/unix/process.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Unix-specific extensions to primitives in the `std::process` module.
2-
32
#![stable(feature = "rust1", since = "1.0.0")]
43

54
use crate::ffi::OsStr;
@@ -10,6 +9,9 @@ use crate::sealed::Sealed;
109
use crate::sys;
1110
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
1211

12+
use crate::sys_common::process_ext;
13+
impl_command_sized! { prelude }
14+
1315
/// Unix-specific extensions to the [`process::Command`] builder.
1416
///
1517
/// This trait is sealed: it cannot be implemented outside the standard library.
@@ -335,3 +337,25 @@ impl IntoRawFd for process::ChildStderr {
335337
pub fn parent_id() -> u32 {
336338
crate::sys::os::getppid()
337339
}
340+
341+
fn maybe_arg_ext(celf: &mut sys::process::Command, arg: impl Arg) -> io::Result<()> {
342+
celf.maybe_arg(arg.to_plain())
343+
}
344+
345+
fn args_ext(
346+
celf: &mut process::Command,
347+
args: impl IntoIterator<Item = impl Arg>,
348+
) -> &mut process::Command {
349+
for arg in args {
350+
celf.arg(arg.to_plain());
351+
}
352+
celf
353+
}
354+
355+
#[unstable(feature = "command_sized", issue = "74549")]
356+
#[cfg(unix)] // doc hack
357+
impl process_ext::CommandSized for process::Command {
358+
impl_command_sized! { marg maybe_arg_ext }
359+
impl_command_sized! { margs maybe_arg_ext }
360+
impl_command_sized! { xargs args_ext }
361+
}

library/std/src/os/windows/process.rs

+56-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,38 @@
22
33
#![stable(feature = "process_extensions", since = "1.2.0")]
44

5+
use crate::ffi::{OsStr, OsString};
6+
use crate::io;
57
use crate::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
68
use crate::process;
79
use crate::sealed::Sealed;
810
use crate::sys;
9-
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
11+
use crate::os::windows::ffi::OsStrExt;
12+
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
13+
pub use crate::sys_common::process_ext::{Arg, Problem};
14+
use crate::sys_common::{process_ext, AsInner, AsInnerMut, FromInner, IntoInner};
15+
use core::convert::TryFrom;
16+
17+
/// Argument type with no escaping.
18+
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
19+
pub struct RawArg<'a>(&'a OsStr);
20+
21+
// FIXME: Inhibiting doc on non-Windows due to mismatching trait methods.
22+
#[cfg(any(windows))]
23+
#[doc(cfg(windows))]
24+
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
25+
impl Arg for RawArg<'_> {
26+
fn append_to(&self, cmd: &mut Vec<u16>, _fq: bool) -> Result<usize, Problem> {
27+
cmd.extend(self.0.encode_wide());
28+
self.arg_size(_fq)
29+
}
30+
fn arg_size(&self, _: bool) -> Result<usize, Problem> {
31+
Ok(self.0.encode_wide().count() + 1)
32+
}
33+
fn to_os_string(&self) -> OsString {
34+
OsStr::to_os_string(&(self.0))
35+
}
36+
}
1037

1138
#[stable(feature = "process_extensions", since = "1.2.0")]
1239
impl FromRawHandle for process::Stdio {
@@ -125,6 +152,13 @@ pub trait CommandExt: Sealed {
125152
/// [2]: <https://msdn.microsoft.com/en-us/library/17w5ykft.aspx>
126153
#[unstable(feature = "windows_process_extensions_force_quotes", issue = "82227")]
127154
fn force_quotes(&mut self, enabled: bool) -> &mut process::Command;
155+
/// Pass an argument with custom escape rules.
156+
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
157+
fn arg_ext(&mut self, arg: impl Arg) -> &mut process::Command;
158+
159+
/// Pass arguments with custom escape rules.
160+
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
161+
fn args_ext(&mut self, args: impl IntoIterator<Item = impl Arg>) -> &mut process::Command;
128162
}
129163

130164
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -138,4 +172,25 @@ impl CommandExt for process::Command {
138172
self.as_inner_mut().force_quotes(enabled);
139173
self
140174
}
175+
176+
fn arg_ext(&mut self, arg: impl Arg) -> &mut process::Command {
177+
self.as_inner_mut().arg_ext(arg);
178+
self
179+
}
180+
181+
fn args_ext(&mut self, args: impl IntoIterator<Item = impl Arg>) -> &mut process::Command {
182+
for arg in args {
183+
self.arg_ext(arg);
184+
}
185+
self
186+
}
187+
}
188+
189+
// FIXME: export maybe_arg_ext so the macro doesn't explicitly reach for as_inner_mut()
190+
#[unstable(feature = "command_sized", issue = "74549")]
191+
#[cfg(windows)] // doc hack
192+
impl process_ext::CommandSized for process::Command {
193+
impl_command_sized! { marg sys::process::Command::maybe_arg_ext }
194+
impl_command_sized! { margs sys::process::Command::maybe_arg_ext }
195+
impl_command_sized! { xargs process::Command::args_ext }
141196
}

library/std/src/sys/unix/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn errno() -> i32 {
7373
}
7474

7575
/// Sets the platform-specific value of errno
76-
#[cfg(all(not(target_os = "linux"), not(target_os = "dragonfly")))] // needed for readdir and syscall!
76+
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks")))] // needed for readdir and syscall!
7777
#[allow(dead_code)] // but not all target cfgs actually end up using it
7878
pub fn set_errno(e: i32) {
7979
unsafe { *errno_location() = e as c_int }

library/std/src/sys/unix/process/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use self::process_common::{Command, CommandArgs, ExitCode, Stdio, StdioPipes};
1+
pub use self::process_common::{Arg, Command, CommandArgs, ExitCode, Problem, Stdio, StdioPipes};
22
pub use self::process_inner::{ExitStatus, Process};
33
pub use crate::ffi::OsString as EnvKey;
44
pub use crate::sys_common::process::CommandEnvs;

0 commit comments

Comments
 (0)