Skip to content

Commit ecc590c

Browse files
committed
Perform target-machine command-line quoting as bytes, not characters
This works because the only characters we care about escaping are ASCII characters, so forwarding non-ASCII UTF-8 bytes as-is still produces correct results.
1 parent ec38671 commit ecc590c

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

compiler/rustc_codegen_llvm/src/back/command_line_args.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,34 @@ mod tests;
66
///
77
/// The result is intended to be informational, for embedding in debug metadata,
88
/// and might not be properly quoted/escaped for actual command-line use.
9-
pub(crate) fn quote_command_line_args(args: &[String]) -> String {
9+
pub(crate) fn quote_command_line_args(args: &[String]) -> Vec<u8> {
1010
// Start with a decent-sized buffer, since rustc invocations tend to be long.
11-
let mut buf = String::with_capacity(128);
11+
let mut buf = Vec::with_capacity(128);
1212

1313
for arg in args {
1414
if !buf.is_empty() {
15-
buf.push(' ');
15+
buf.push(b' ');
1616
}
1717

1818
print_arg_quoted(&mut buf, arg);
1919
}
2020

21+
debug_assert!(str::from_utf8(&buf).is_ok());
22+
2123
buf
2224
}
2325

2426
/// Equivalent to LLVM's `sys::printArg` with quoting always enabled
2527
/// (see llvm/lib/Support/Program.cpp).
26-
fn print_arg_quoted(buf: &mut String, arg: &str) {
28+
fn print_arg_quoted(buf: &mut Vec<u8>, arg: &str) {
2729
buf.reserve(arg.len() + 2);
2830

29-
buf.push('"');
30-
for ch in arg.chars() {
31-
if matches!(ch, '"' | '\\' | '$') {
32-
buf.push('\\');
31+
buf.push(b'"');
32+
for &byte in arg.as_bytes() {
33+
if matches!(byte, b'"' | b'\\' | b'$') {
34+
buf.push(b'\\');
3335
}
34-
buf.push(ch);
36+
buf.push(byte);
3537
}
36-
buf.push('"');
38+
buf.push(b'"');
3739
}

compiler/rustc_codegen_llvm/src/back/command_line_args/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ fn quote_command_line_args() {
2020
for &Case { args, expected } in cases {
2121
let args = args.iter().copied().map(str::to_owned).collect::<Vec<_>>();
2222
let actual = quote_command_line_args(&args);
23-
assert_eq!(actual, expected, "args {args:?}");
23+
assert_eq!(str::from_utf8(&actual), Ok(expected), "args {args:?}");
2424
}
2525
}

compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl OwnedTargetMachine {
3939
debug_info_compression: &CStr,
4040
use_emulated_tls: bool,
4141
argv0: &str,
42-
command_line_args: &str,
42+
command_line_args: &[u8],
4343
use_wasm_eh: bool,
4444
) -> Result<Self, LlvmError<'static>> {
4545
// SAFETY: llvm::LLVMRustCreateTargetMachine copies pointed to data

0 commit comments

Comments
 (0)