Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit b068b31

Browse files
committed
use OsString for Command.arguments
1 parent b0897c6 commit b068b31

File tree

6 files changed

+32
-28
lines changed

6 files changed

+32
-28
lines changed

src/protocol/command.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use super::argument_parser::Parser;
22
use super::executable_path;
33
use crate::R;
4+
use std::ffi::OsString;
45
use std::os::unix::ffi::OsStrExt;
56
use std::path::PathBuf;
67
use std::str;
78

89
#[derive(PartialEq, Eq, Debug, Clone)]
910
pub struct Command {
1011
pub executable: PathBuf,
11-
pub arguments: Vec<Vec<u8>>,
12+
pub arguments: Vec<OsString>,
1213
}
1314

1415
impl Command {
@@ -39,10 +40,10 @@ impl Command {
3940
word.chars().map(escape_char).collect::<Vec<_>>().join("")
4041
}
4142

42-
pub fn format_arguments(arguments: Vec<Vec<u8>>) -> String {
43+
pub fn format_arguments(arguments: Vec<OsString>) -> String {
4344
arguments
4445
.into_iter()
45-
.map(|argument| Command::escape(String::from_utf8_lossy(&argument).to_string()))
46+
.map(|argument| Command::escape(argument.to_string_lossy().into_owned()))
4647
.map(Command::add_quotes_if_needed)
4748
.collect::<Vec<String>>()
4849
.join(" ")
@@ -69,7 +70,7 @@ impl Command {
6970
match words.next() {
7071
Some(executable) => Ok(Command {
7172
executable: PathBuf::from(executable),
72-
arguments: words.map(|word| word.into_bytes()).collect(),
73+
arguments: words.map(OsString::from).collect(),
7374
}),
7475
None => Err(format!(
7576
"expected: space-separated command and arguments ({:?})",
@@ -94,7 +95,7 @@ mod command {
9495
Command::new("foo bar")?,
9596
Command {
9697
executable: PathBuf::from("foo"),
97-
arguments: vec![b"bar".to_vec()]
98+
arguments: vec![OsString::from("bar")]
9899
}
99100
);
100101
Ok(())
@@ -106,7 +107,7 @@ mod command {
106107
Command::new(r#"foo "bar baz""#)?,
107108
Command {
108109
executable: PathBuf::from("foo"),
109-
arguments: vec![b"bar baz".to_vec()]
110+
arguments: vec![OsString::from("bar baz")]
110111
}
111112
);
112113
Ok(())
@@ -118,14 +119,14 @@ mod command {
118119
Command::new(r#"foo\" bar baz"#)?,
119120
Command {
120121
executable: PathBuf::from("foo\""),
121-
arguments: vec![b"bar", b"baz"].map(|arg| arg.to_vec())
122+
arguments: vec!["bar", "baz"].map(OsString::from)
122123
}
123124
);
124125
assert_eq!(
125126
Command::new(r#"foo\" "bar baz""#)?,
126127
Command {
127128
executable: PathBuf::from("foo\""),
128-
arguments: vec![b"bar baz".to_vec()]
129+
arguments: vec![OsString::from("bar baz")]
129130
}
130131
);
131132
Ok(())
@@ -137,7 +138,7 @@ mod command {
137138
Command::new(r#"foo "bar\" baz""#)?,
138139
Command {
139140
executable: PathBuf::from("foo"),
140-
arguments: vec![b"bar\" baz".to_vec()]
141+
arguments: vec![OsString::from("bar\" baz")]
141142
}
142143
);
143144
Ok(())
@@ -176,7 +177,7 @@ mod command {
176177
Command::new("foo bar")?,
177178
Command {
178179
executable: PathBuf::from("foo"),
179-
arguments: vec![b"bar".to_vec()]
180+
arguments: vec![OsString::from("bar")]
180181
}
181182
);
182183
Ok(())
@@ -188,7 +189,7 @@ mod command {
188189
Command::new(" foo bar")?,
189190
Command {
190191
executable: PathBuf::from("foo"),
191-
arguments: vec![b"bar".to_vec()]
192+
arguments: vec![OsString::from("bar")]
192193
}
193194
);
194195
Ok(())
@@ -200,7 +201,7 @@ mod command {
200201
Command::new("foo bar ")?,
201202
Command {
202203
executable: PathBuf::from("foo"),
203-
arguments: vec![b"bar".to_vec()]
204+
arguments: vec![OsString::from("bar")]
204205
}
205206
);
206207
Ok(())
@@ -215,7 +216,7 @@ mod command {
215216
Command::new(r#"foo "bar\nbaz""#)?,
216217
Command {
217218
executable: PathBuf::from("foo"),
218-
arguments: vec![b"bar\nbaz".to_vec()]
219+
arguments: vec![OsString::from("bar\nbaz")]
219220
}
220221
);
221222
Ok(())
@@ -227,7 +228,7 @@ mod command {
227228
Command::new(r#"foo bar\ baz"#)?,
228229
Command {
229230
executable: PathBuf::from("foo"),
230-
arguments: vec![b"bar baz".to_vec()]
231+
arguments: vec![OsString::from("bar baz")]
231232
}
232233
);
233234
Ok(())
@@ -239,7 +240,7 @@ mod command {
239240
Command::new(r#"foo bar\\baz"#)?,
240241
Command {
241242
executable: PathBuf::from("foo"),
242-
arguments: vec![br#"bar\baz"#.to_vec()]
243+
arguments: vec![OsString::from(r"bar\baz")]
243244
}
244245
);
245246
Ok(())

src/protocol/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::R;
1313
pub use command::Command;
1414
use linked_hash_map::LinkedHashMap;
1515
use std::collections::{HashMap, VecDeque};
16+
use std::ffi::OsString;
1617
use std::fs;
1718
use std::path::{Path, PathBuf};
1819
use yaml_rust::{yaml::Hash, Yaml, YamlLoader};
@@ -111,7 +112,7 @@ mod parse_step {
111112
test_parse_step(r#""foo bar""#)?.command,
112113
Command {
113114
executable: PathBuf::from("foo"),
114-
arguments: vec![b"bar".to_vec()],
115+
arguments: vec![OsString::from("bar")],
115116
},
116117
);
117118
Ok(())
@@ -135,7 +136,7 @@ mod parse_step {
135136
test_parse_step(r#"{command: "foo bar"}"#)?.command,
136137
Command {
137138
executable: PathBuf::from("foo"),
138-
arguments: vec![b"bar".to_vec()],
139+
arguments: vec![OsString::from("bar")],
139140
},
140141
);
141142
Ok(())
@@ -317,11 +318,7 @@ impl Protocol {
317318
fn serialize(&self) -> Yaml {
318319
let mut protocol = LinkedHashMap::new();
319320
if !self.arguments.is_empty() {
320-
let arguments = self
321-
.arguments
322-
.iter()
323-
.map(|arg| arg.as_bytes().to_vec())
324-
.collect();
321+
let arguments = self.arguments.iter().map(OsString::from).collect();
325322
protocol.insert(
326323
Yaml::from_str("arguments"),
327324
Yaml::String(Command::format_arguments(arguments)),
@@ -551,7 +548,7 @@ mod load {
551548
)?
552549
.steps
553550
.map(|step| step.command.arguments),
554-
vec![vec![b"foo".to_vec(), b"bar".to_vec()]],
551+
vec![vec![OsString::from("foo"), OsString::from("bar")]],
555552
);
556553
Ok(())
557554
}

src/protocol_checker/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use checker_result::CheckerResult;
1212
use libc::{c_ulonglong, user_regs_struct};
1313
use nix::sys::ptrace;
1414
use nix::unistd::Pid;
15+
use std::ffi::OsString;
1516
use std::os::unix::ffi::OsStrExt;
1617
use std::path::PathBuf;
1718

@@ -98,7 +99,7 @@ impl SyscallMock for ProtocolChecker {
9899
pid: Pid,
99100
registers: &user_regs_struct,
100101
executable: PathBuf,
101-
arguments: Vec<Vec<u8>>,
102+
arguments: Vec<OsString>,
102103
) -> R<()> {
103104
let is_unmocked_command = self.unmocked_commands.iter().any(|unmocked_command| {
104105
protocol::compare_executables(

src/recorder/hole_recorder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::tracer::SyscallMock;
99
use crate::{ExitCode, R};
1010
use libc::user_regs_struct;
1111
use nix::unistd::Pid;
12+
use std::ffi::OsString;
1213
use std::path::{Path, PathBuf};
1314

1415
pub enum HoleRecorder {
@@ -42,7 +43,7 @@ impl SyscallMock for HoleRecorder {
4243
pid: Pid,
4344
registers: &user_regs_struct,
4445
executable: PathBuf,
45-
arguments: Vec<Vec<u8>>,
46+
arguments: Vec<OsString>,
4647
) -> R<()> {
4748
match self {
4849
HoleRecorder::Checker {

src/recorder/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::tracer::SyscallMock;
88
use crate::R;
99
use libc::user_regs_struct;
1010
use nix::unistd::Pid;
11+
use std::ffi::OsString;
1112
use std::os::unix::ffi::OsStrExt;
1213
use std::path::PathBuf;
1314

@@ -43,7 +44,7 @@ impl SyscallMock for Recorder {
4344
_pid: Pid,
4445
_registers: &user_regs_struct,
4546
executable: PathBuf,
46-
arguments: Vec<Vec<u8>>,
47+
arguments: Vec<OsString>,
4748
) -> R<()> {
4849
let is_unmocked_command = self.unmocked_commands.iter().any(|unmocked_command| {
4950
compare_executables(

src/tracer/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait SyscallMock {
3737
_pid: Pid,
3838
_registers: &user_regs_struct,
3939
_executable: PathBuf,
40-
_arguments: Vec<Vec<u8>>,
40+
_arguments: Vec<OsString>,
4141
) -> R<()> {
4242
Ok(())
4343
}
@@ -246,7 +246,10 @@ impl Tracer {
246246
pid,
247247
registers.rdi,
248248
)?));
249-
let arguments = tracee_memory::peek_string_array(pid, registers.rsi)?;
249+
let arguments = tracee_memory::peek_string_array(pid, registers.rsi)?
250+
.into_iter()
251+
.map(OsString::from_vec)
252+
.collect();
250253
syscall_mock.handle_execve_enter(pid, registers, executable, arguments)?;
251254
}
252255
}

0 commit comments

Comments
 (0)