From 90013295aa779e6df7a51bf9b37daf201a821e3f Mon Sep 17 00:00:00 2001 From: Tomoki Aonuma Date: Fri, 25 Mar 2022 01:57:03 +0900 Subject: [PATCH] Fix parse_string_strict() to detect unbalanced quotes properly (#4928) --- crates/nu-parser/src/parser.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 6c0434ee89..2ea697d9c4 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2391,17 +2391,28 @@ pub fn parse_string_strict( let bytes = working_set.get_span_contents(span); // Check for unbalanced quotes: - if (bytes.starts_with(b"\"") || (bytes.starts_with(b"$\""))) && !bytes.ends_with(b"\"") { - return (garbage(span), Some(ParseError::Unclosed("\"".into(), span))); - } - if (bytes.starts_with(b"\'") || (bytes.starts_with(b"$\""))) && !bytes.ends_with(b"\'") { - return (garbage(span), Some(ParseError::Unclosed("\'".into(), span))); + { + let bytes = if bytes.starts_with(b"$") { + &bytes[1..] + } else { + bytes + }; + if bytes.starts_with(b"\"") && (bytes.len() == 1 || !bytes.ends_with(b"\"")) { + return (garbage(span), Some(ParseError::Unclosed("\"".into(), span))); + } + if bytes.starts_with(b"\'") && (bytes.len() == 1 || !bytes.ends_with(b"\'")) { + return (garbage(span), Some(ParseError::Unclosed("\'".into(), span))); + } } let (bytes, quoted) = if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1) || (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1) { (&bytes[1..(bytes.len() - 1)], true) + } else if (bytes.starts_with(b"$\"") && bytes.ends_with(b"\"") && bytes.len() > 2) + || (bytes.starts_with(b"$\'") && bytes.ends_with(b"\'") && bytes.len() > 2) + { + (&bytes[2..(bytes.len() - 1)], true) } else { (bytes, false) };