Skip to content

Commit 43ed1eb

Browse files
committed
use std escape fn
1 parent f862372 commit 43ed1eb

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

plugins/updater/src/updater.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,19 +1371,38 @@ impl PathExt for PathBuf {
13711371
}
13721372
}
13731373

1374+
// adapted from https://github.com/rust-lang/rust/blob/1c047506f94cd2d05228eb992b0a6bbed1942349/library/std/src/sys/args/windows.rs#L174
13741375
#[cfg(windows)]
13751376
fn escape_nsis_current_exe_arg(arg: &&OsStr) -> String {
1376-
let mut arg = arg.to_string_lossy().to_string();
1377+
let arg = arg.to_string_lossy();
1378+
let mut cmd: Vec<char> = Vec::new();
13771379

1378-
/* if arg.contains('"') {
1379-
arg = arg.replace('"', r#""""#);
1380-
} */
1381-
1382-
if arg.contains(' ') || arg.contains('\t') || arg.contains('/') {
1383-
arg = format!("\"{arg}\"");
1380+
let quote = arg.chars().any(|c| c == ' ' || c == '\t' || c == '/') || arg.is_empty();
1381+
let escape = true;
1382+
if quote {
1383+
cmd.push('"');
13841384
}
1385-
1386-
arg
1385+
let mut backslashes: usize = 0;
1386+
for x in arg.chars() {
1387+
if escape {
1388+
if x == '\\' {
1389+
backslashes += 1;
1390+
} else {
1391+
if x == '"' {
1392+
// Add n+1 backslashes to total 2n+1 before internal '"'.
1393+
cmd.extend((0..=backslashes).map(|_| '\\'));
1394+
}
1395+
backslashes = 0;
1396+
}
1397+
}
1398+
cmd.push(x);
1399+
}
1400+
if quote {
1401+
// Add n backslashes to total 2n before ending '"'.
1402+
cmd.extend((0..backslashes).map(|_| '\\'));
1403+
cmd.push('"');
1404+
}
1405+
cmd.into_iter().collect()
13871406
}
13881407

13891408
#[cfg(windows)]

0 commit comments

Comments
 (0)