From e052fac4f325236f88ef225ed3c3cc96a02fbfa8 Mon Sep 17 00:00:00 2001 From: zhangyao2022 Date: Mon, 14 Aug 2023 18:33:47 +0800 Subject: [PATCH] fix: Fixed parsing single quotes error --- core/libcore/src/exec/cmd.rs | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/core/libcore/src/exec/cmd.rs b/core/libcore/src/exec/cmd.rs index 8f9c62f9..60427397 100755 --- a/core/libcore/src/exec/cmd.rs +++ b/core/libcore/src/exec/cmd.rs @@ -173,8 +173,24 @@ fn parse_exec(s: &str) -> Result> { let mut argv: Vec = Vec::new(); let mut cur = String::new(); let mut found_semicolon_wait_space = false; + let mut found_single_quote = false; for c in content.chars() { argv_start += 1; + + if found_single_quote && c != '\'' { + cur += &c.to_string(); + continue; + } + if c == '\'' { + if found_single_quote { + argv.push(cur); + cur = "".to_string(); + found_single_quote = false; + continue; + } + found_single_quote = true; + continue; + } if c == ' ' { /* now we find " ; ", break the loop */ if found_semicolon_wait_space { @@ -205,6 +221,12 @@ fn parse_exec(s: &str) -> Result> { found_semicolon_wait_space = false; cur += &c.to_string(); } + + if found_single_quote { + return Err(Error::Invalid { + what: "no valid exec command, wrong single quote".to_string(), + }); + } /* No more characters after " ;", drop current argv */ if found_semicolon_wait_space { cur = String::new(); @@ -395,5 +417,23 @@ mod tests { assert!(parse_exec(&path).is_err()); assert!(parse_exec("/bin/echo good ; ; ; ;").is_err()); + assert!(parse_exec("/bin/echo 'good1 good2").is_err()); + assert!(parse_exec("/bin/echo 'good good1' 'good2").is_err()); + assert_eq!( + parse_exec("/bin/echo 'good good1' good2").unwrap(), + VecDeque::from([ExecCommand { + path: "/bin/echo".to_string(), + argv: vec!["good good1".to_string(), "good2".to_string()], + flags: ExecFlag::EXEC_COMMAND_EMPTY + }]) + ); + assert_eq!( + parse_exec("/bin/echo 'good good1' 'good2'").unwrap(), + VecDeque::from([ExecCommand { + path: "/bin/echo".to_string(), + argv: vec!["good good1".to_string(), "good2".to_string()], + flags: ExecFlag::EXEC_COMMAND_EMPTY + }]) + ); } } -- 2.30.2