Fix for escaping backslashes in interpolated strings (fixes #6737) (#7020)

Co-authored-by: Gavin Foley <gavinmfoley@gmail.com>
This commit is contained in:
Gavin Foley 2022-11-06 14:57:28 -05:00 committed by GitHub
parent 5ea245badf
commit d4798d6ee1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 257 additions and 105 deletions

View File

@ -1604,14 +1604,24 @@ pub fn parse_string_interpolation(
let mut b = start; let mut b = start;
let mut consecutive_backslashes: usize = 0;
while b != end { while b != end {
if contents[b - start] == b'(' let current_byte = contents[b - start];
&& (if double_quote && (b - start) > 0 {
contents[b - start - 1] != b'\\' match mode {
InterpolationMode::String => {
let preceding_consecutive_backslashes = consecutive_backslashes;
let is_backslash = current_byte == b'\\';
consecutive_backslashes = if is_backslash {
preceding_consecutive_backslashes + 1
} else { } else {
true 0
}) };
&& mode == InterpolationMode::String
if current_byte == b'('
&& (!double_quote || preceding_consecutive_backslashes % 2 == 0)
{ {
mode = InterpolationMode::Expression; mode = InterpolationMode::Expression;
if token_start < b { if token_start < b {
@ -1639,8 +1649,9 @@ pub fn parse_string_interpolation(
token_start = b; token_start = b;
} }
} }
if mode == InterpolationMode::Expression { }
let byte = contents[b - start]; InterpolationMode::Expression => {
let byte = current_byte;
if let Some(b'\'') = delimiter_stack.last() { if let Some(b'\'') = delimiter_stack.last() {
if byte == b'\'' { if byte == b'\'' {
delimiter_stack.pop(); delimiter_stack.pop();
@ -1674,8 +1685,12 @@ pub fn parse_string_interpolation(
end: b + 1, end: b + 1,
}; };
let (expr, err) = let (expr, err) = parse_full_cell_path(
parse_full_cell_path(working_set, None, span, expand_aliases_denylist); working_set,
None,
span,
expand_aliases_denylist,
);
error = error.or(err); error = error.or(err);
output.push(expr); output.push(expr);
} }
@ -1685,6 +1700,8 @@ pub fn parse_string_interpolation(
} }
} }
} }
}
b += 1; b += 1;
} }

View File

@ -175,46 +175,6 @@ pub fn parse_binary_with_multi_byte_char() {
assert!(!matches!(&expressions[0].expr, Expr::Binary(_))) assert!(!matches!(&expressions[0].expr, Expr::Binary(_)))
} }
#[test]
pub fn parse_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"\"hello nushell\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
#[test]
pub fn parse_escaped_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"\"hello \\u006e\\u0075\\u0073hell\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
#[test] #[test]
pub fn parse_call() { pub fn parse_call() {
let engine_state = EngineState::new(); let engine_state = EngineState::new();
@ -364,6 +324,181 @@ fn test_nothing_comparisson_neq() {
)) ))
} }
mod string {
use super::*;
#[test]
pub fn parse_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"\"hello nushell\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
#[test]
pub fn parse_escaped_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"\"hello \\u006e\\u0075\\u0073hell\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
mod interpolation {
use super::*;
#[test]
pub fn parse_string_interpolation() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"$\"hello (39 + 3)\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 2);
assert_eq!(subexprs[0], &Expr::String("hello ".to_string()));
assert!(matches!(subexprs[1], &Expr::FullCellPath(..)));
}
#[test]
pub fn parse_string_interpolation_escaped_parenthesis() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"$\"hello \\(39 + 3)\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 1);
assert_eq!(subexprs[0], &Expr::String("hello (39 + 3)".to_string()));
}
#[test]
pub fn parse_string_interpolation_escaped_backslash_before_parenthesis() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"$\"hello \\\\(39 + 3)\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 2);
assert_eq!(subexprs[0], &Expr::String("hello \\".to_string()));
assert!(matches!(subexprs[1], &Expr::FullCellPath(..)));
}
#[test]
pub fn parse_string_interpolation_backslash_count_reset_by_expression() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"$\"\\(1 + 3)\\(7 - 5)\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 1);
assert_eq!(subexprs[0], &Expr::String("(1 + 3)(7 - 5)".to_string()));
}
}
}
mod range { mod range {
use super::*; use super::*;
use nu_protocol::ast::{RangeInclusion, RangeOperator}; use nu_protocol::ast::{RangeInclusion, RangeOperator};