Skip to content

Commit 0f923a4

Browse files
Rollup merge of rust-lang#123709 - tgross35:windows-cmd-docs-update, r=ChrisDenton
Update documentation related to the recent cmd.exe fix Fix some grammar nits, change `bat` (extension) -> `batch` (file), and make line wrapping more consistent.
2 parents 7d2a95b + a7238b9 commit 0f923a4

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

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

+32-17
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ pub trait CommandExt: Sealed {
199199

200200
/// Append literal text to the command line without any quoting or escaping.
201201
///
202-
/// This is useful for passing arguments to applications which doesn't follow
202+
/// This is useful for passing arguments to applications that don't follow
203203
/// the standard C run-time escaping rules, such as `cmd.exe /c`.
204204
///
205-
/// # Bat files
205+
/// # Batch files
206206
///
207-
/// Note the `cmd /c` command line has slightly different escaping rules then bat files
207+
/// Note the `cmd /c` command line has slightly different escaping rules than batch files
208208
/// themselves. If possible, it may be better to write complex arguments to a temporary
209-
/// .bat file, with appropriate escaping, and simply run that using:
209+
/// `.bat` file, with appropriate escaping, and simply run that using:
210210
///
211211
/// ```no_run
212212
/// # use std::process::Command;
@@ -217,7 +217,7 @@ pub trait CommandExt: Sealed {
217217
///
218218
/// # Example
219219
///
220-
/// Run a bat script using both trusted and untrusted arguments.
220+
/// Run a batch script using both trusted and untrusted arguments.
221221
///
222222
/// ```no_run
223223
/// #[cfg(windows)]
@@ -241,9 +241,10 @@ pub trait CommandExt: Sealed {
241241
/// if !user_name.chars().all(|c| c.is_alphanumeric()) {
242242
/// return Err(Error::new(ErrorKind::InvalidInput, "invalid user name"));
243243
/// }
244-
/// // now we've checked the user name, let's add that too.
245-
/// cmd_args.push(' ');
246-
/// cmd_args.push_str(&format!("--user {user_name}"));
244+
///
245+
/// // now we have validated the user name, let's add that too.
246+
/// cmd_args.push_str(" --user ");
247+
/// cmd_args.push_str(user_name);
247248
///
248249
/// // call cmd.exe and return the output
249250
/// Command::new("cmd.exe")
@@ -287,25 +288,37 @@ pub trait CommandExt: Sealed {
287288
#[unstable(feature = "windows_process_extensions_async_pipes", issue = "98289")]
288289
fn async_pipes(&mut self, always_async: bool) -> &mut process::Command;
289290

290-
/// Sets a raw attribute on the command, providing extended configuration options for Windows processes.
291+
/// Set a raw attribute on the command, providing extended configuration options for Windows
292+
/// processes.
293+
///
294+
/// This method allows you to specify custom attributes for a child process on Windows systems
295+
/// using raw attribute values. Raw attributes provide extended configurability for process
296+
/// creation, but their usage can be complex and potentially unsafe.
291297
///
292-
/// This method allows you to specify custom attributes for a child process on Windows systems using raw attribute values.
293-
/// Raw attributes provide extended configurability for process creation, but their usage can be complex and potentially unsafe.
298+
/// The `attribute` parameter specifies the raw attribute to be set, while the `value`
299+
/// parameter holds the value associated with that attribute. Please refer to the
300+
/// [`windows-rs` documentation] or the [Win32 API documentation] for detailed information
301+
/// about available attributes and their meanings.
294302
///
295-
/// The `attribute` parameter specifies the raw attribute to be set, while the `value` parameter holds the value associated with that attribute.
296-
/// Please refer to the [`windows-rs`](https://microsoft.github.io/windows-docs-rs/doc/windows/) documentation or the [`Win32 API documentation`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute) for detailed information about available attributes and their meanings.
303+
/// [`windows-rs` documentation]: https://microsoft.github.io/windows-docs-rs/doc/windows/
304+
/// [Win32 API documentation]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute
297305
///
298306
/// # Note
299307
///
300308
/// The maximum number of raw attributes is the value of [`u32::MAX`].
301-
/// If this limit is exceeded, the call to [`process::Command::spawn`] will return an `Error` indicating that the maximum number of attributes has been exceeded.
309+
/// If this limit is exceeded, the call to [`process::Command::spawn`] will return an `Error`
310+
/// indicating that the maximum number of attributes has been exceeded.
311+
///
302312
/// # Safety
303313
///
304-
/// The usage of raw attributes is potentially unsafe and should be done with caution. Incorrect attribute values or improper configuration can lead to unexpected behavior or errors.
314+
/// The usage of raw attributes is potentially unsafe and should be done with caution.
315+
/// Incorrect attribute values or improper configuration can lead to unexpected behavior or
316+
/// errors.
305317
///
306318
/// # Example
307319
///
308-
/// The following example demonstrates how to create a child process with a specific parent process ID using a raw attribute.
320+
/// The following example demonstrates how to create a child process with a specific parent
321+
/// process ID using a raw attribute.
309322
///
310323
/// ```rust
311324
/// #![feature(windows_process_extensions_raw_attribute)]
@@ -339,7 +352,9 @@ pub trait CommandExt: Sealed {
339352
///
340353
/// # Safety Note
341354
///
342-
/// Remember that improper use of raw attributes can lead to undefined behavior or security vulnerabilities. Always consult the documentation and ensure proper attribute values are used.
355+
/// Remember that improper use of raw attributes can lead to undefined behavior or security
356+
/// vulnerabilities. Always consult the documentation and ensure proper attribute values are
357+
/// used.
343358
#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
344359
unsafe fn raw_attribute<T: Copy + Send + Sync + 'static>(
345360
&mut self,

library/std/src/process.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
//!
9191
//! # Windows argument splitting
9292
//!
93-
//! On Unix systems arguments are passed to a new process as an array of strings
94-
//! but on Windows arguments are passed as a single commandline string and it's
93+
//! On Unix systems arguments are passed to a new process as an array of strings,
94+
//! but on Windows arguments are passed as a single commandline string and it is
9595
//! up to the child process to parse it into an array. Therefore the parent and
9696
//! child processes must agree on how the commandline string is encoded.
9797
//!
@@ -107,26 +107,26 @@
107107
//! * Use [`raw_arg`] to build a custom commandline. This bypasses the escaping
108108
//! rules used by [`arg`] so should be used with due caution.
109109
//!
110-
//! `cmd.exe` and `.bat` use non-standard argument parsing and are especially
110+
//! `cmd.exe` and `.bat` files use non-standard argument parsing and are especially
111111
//! vulnerable to malicious input as they may be used to run arbitrary shell
112112
//! commands. Untrusted arguments should be restricted as much as possible.
113113
//! For examples on handling this see [`raw_arg`].
114114
//!
115-
//! ### Bat file special handling
115+
//! ### Batch file special handling
116116
//!
117117
//! On Windows, `Command` uses the Windows API function [`CreateProcessW`] to
118-
//! spawn new processes. An undocumented feature of this function is that,
118+
//! spawn new processes. An undocumented feature of this function is that
119119
//! when given a `.bat` file as the application to run, it will automatically
120-
//! convert that into running `cmd.exe /c` with the bat file as the next argument.
120+
//! convert that into running `cmd.exe /c` with the batch file as the next argument.
121121
//!
122122
//! For historical reasons Rust currently preserves this behaviour when using
123123
//! [`Command::new`], and escapes the arguments according to `cmd.exe` rules.
124124
//! Due to the complexity of `cmd.exe` argument handling, it might not be
125-
//! possible to safely escape some special chars, and using them will result
125+
//! possible to safely escape some special characters, and using them will result
126126
//! in an error being returned at process spawn. The set of unescapeable
127-
//! special chars might change between releases.
127+
//! special characters might change between releases.
128128
//!
129-
//! Also note that running `.bat` scripts in this way may be removed in the
129+
//! Also note that running batch scripts in this way may be removed in the
130130
//! future and so should not be relied upon.
131131
//!
132132
//! [`spawn`]: Command::spawn
@@ -659,16 +659,19 @@ impl Command {
659659
///
660660
/// Note that the argument is not passed through a shell, but given
661661
/// literally to the program. This means that shell syntax like quotes,
662-
/// escaped characters, word splitting, glob patterns, variable substitution, etc.
663-
/// have no effect.
662+
/// escaped characters, word splitting, glob patterns, variable substitution,
663+
/// etc. have no effect.
664664
///
665665
/// <div class="warning">
666666
///
667-
/// On Windows use caution with untrusted inputs. Most applications use the
668-
/// standard convention for decoding arguments passed to them. These are safe to use with `arg`.
669-
/// However some applications, such as `cmd.exe` and `.bat` files, use a non-standard way of decoding arguments
670-
/// and are therefore vulnerable to malicious input.
671-
/// In the case of `cmd.exe` this is especially important because a malicious argument can potentially run arbitrary shell commands.
667+
/// On Windows, use caution with untrusted inputs. Most applications use the
668+
/// standard convention for decoding arguments passed to them. These are safe to
669+
/// use with `arg`. However, some applications such as `cmd.exe` and `.bat` files
670+
/// use a non-standard way of decoding arguments. They are therefore vulnerable
671+
/// to malicious input.
672+
///
673+
/// In the case of `cmd.exe` this is especially important because a malicious
674+
/// argument can potentially run arbitrary shell commands.
672675
///
673676
/// See [Windows argument splitting][windows-args] for more details
674677
/// or [`raw_arg`] for manually implementing non-standard argument encoding.
@@ -710,11 +713,14 @@ impl Command {
710713
///
711714
/// <div class="warning">
712715
///
713-
/// On Windows use caution with untrusted inputs. Most applications use the
714-
/// standard convention for decoding arguments passed to them. These are safe to use with `args`.
715-
/// However some applications, such as `cmd.exe` and `.bat` files, use a non-standard way of decoding arguments
716-
/// and are therefore vulnerable to malicious input.
717-
/// In the case of `cmd.exe` this is especially important because a malicious argument can potentially run arbitrary shell commands.
716+
/// On Windows, use caution with untrusted inputs. Most applications use the
717+
/// standard convention for decoding arguments passed to them. These are safe to
718+
/// use with `arg`. However, some applications such as `cmd.exe` and `.bat` files
719+
/// use a non-standard way of decoding arguments. They are therefore vulnerable
720+
/// to malicious input.
721+
///
722+
/// In the case of `cmd.exe` this is especially important because a malicious
723+
/// argument can potentially run arbitrary shell commands.
718724
///
719725
/// See [Windows argument splitting][windows-args] for more details
720726
/// or [`raw_arg`] for manually implementing non-standard argument encoding.

0 commit comments

Comments
 (0)