Skip to content

Commit 8d5c7fe

Browse files
committed
fix(parser): use checked_sub in parse_range to avoid overflow
1 parent 11d71fe commit 8d5c7fe

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

crates/nu-parser/src/parser.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,9 +1916,12 @@ pub fn parse_range(working_set: &mut StateWorkingSet, span: Span) -> Option<Expr
19161916
.filter_map(|(pos, _)| {
19171917
// paren_depth = count of unclosed parens prior to pos
19181918
let before = &token[..pos];
1919-
let paren_depth = before.chars().filter(|&c| c == '(').count()
1920-
- before.chars().filter(|&c| c == ')').count();
1921-
if paren_depth == 0 { Some(pos) } else { None }
1919+
let paren_depth = before
1920+
.chars()
1921+
.filter(|&c| c == '(')
1922+
.count()
1923+
.checked_sub(before.chars().filter(|&c| c == ')').count());
1924+
paren_depth.and_then(|d| (d == 0).then_some(pos))
19221925
})
19231926
.collect();
19241927

tests/parsing/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,10 @@ fn wacky_range_parse_comb() {
716716
let actual = nu!(r#"1..(5..10 | first)..10"#);
717717
assert!(actual.err.is_empty());
718718
}
719+
720+
// Regression test https://github.com/nushell/nushell/issues/17146
721+
#[test]
722+
fn wacky_range_unmatched_paren() {
723+
let actual = nu!(r#"') .."#);
724+
assert!(!actual.err.is_empty());
725+
}

0 commit comments

Comments
 (0)