75 lines
2.3 KiB
Diff
75 lines
2.3 KiB
Diff
From 146ea5d4ada08dcc237ba759d19e2729511d46e8 Mon Sep 17 00:00:00 2001
|
|
From: Cody P Schafer <dev@codyps.com>
|
|
Date: Wed, 26 Nov 2014 10:00:32 -0500
|
|
Subject: [PATCH 01/12] libstd/io/process/Command: fully quote and escape the
|
|
command and all args
|
|
|
|
This makes the command (which may have trailing or leading white space
|
|
the user does not expect) unambiguous.
|
|
|
|
It also makes any usage of a literal ' (single quote) in arguments or
|
|
the command name unambiguous by escaping them in the same style as posix
|
|
shell.
|
|
---
|
|
src/libstd/old_io/process.rs | 31 ++++++++++++++++++++++++++++---
|
|
1 file changed, 28 insertions(+), 3 deletions(-)
|
|
|
|
diff --git a/src/libstd/old_io/process.rs b/src/libstd/old_io/process.rs
|
|
index a13295b..fe12546 100644
|
|
--- a/src/libstd/old_io/process.rs
|
|
+++ b/src/libstd/old_io/process.rs
|
|
@@ -30,6 +30,7 @@ use sync::mpsc::{channel, Receiver};
|
|
use sys::fs::FileDesc;
|
|
use sys::process::Process as ProcessImp;
|
|
use sys;
|
|
+use string::CowString;
|
|
use thread;
|
|
|
|
#[cfg(windows)] use hash;
|
|
@@ -394,15 +395,39 @@ impl Command {
|
|
}
|
|
}
|
|
|
|
+struct SingleQuotedStr<'a> {
|
|
+ s: CowString<'a>
|
|
+}
|
|
+
|
|
+impl<'b> SingleQuotedStr<'b> {
|
|
+ fn new<'a>(i: CowString<'a>) -> SingleQuotedStr<'a> {
|
|
+ SingleQuotedStr { s: i }
|
|
+ }
|
|
+}
|
|
+
|
|
+impl<'a> fmt::Debug for SingleQuotedStr<'a> {
|
|
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
+ let mut elems = self.s.split('\'');
|
|
+ let fst = elems.next().unwrap_or("");
|
|
+ try!(write!(f, "'{:?}", fst));
|
|
+ for elem in elems {
|
|
+ try!(write!(f, "'\\''{:?}", elem));
|
|
+ }
|
|
+ write!(f, "'")
|
|
+ }
|
|
+}
|
|
+
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
impl fmt::Debug for Command {
|
|
/// Format the program and arguments of a Command for display. Any
|
|
/// non-utf8 data is lossily converted using the utf8 replacement
|
|
/// character.
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
- try!(write!(f, "{:?}", self.program));
|
|
- for arg in &self.args {
|
|
- try!(write!(f, " '{:?}'", arg));
|
|
+ try!(write!(f, "{:?}", SingleQuotedStr::new(String::from_utf8_lossy(
|
|
+ self.program.as_bytes()))));
|
|
+ for arg in self.args.iter() {
|
|
+ try!(write!(f, " {:?}", SingleQuotedStr::new(String::from_utf8_lossy(
|
|
+ arg.as_bytes()))));
|
|
}
|
|
Ok(())
|
|
}
|
|
--
|
|
2.3.1
|
|
|