mirror of
https://github.com/nushell/nushell.git
synced 2024-11-07 09:04:18 +01:00
Fix parser panic (#5820)
This commit is contained in:
parent
28c21121cf
commit
2caa44cea8
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2685,6 +2685,7 @@ name = "nu-parser"
|
||||
version = "0.64.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"itertools",
|
||||
"log",
|
||||
"miette 4.7.1",
|
||||
"nu-path",
|
||||
|
@ -8,6 +8,7 @@ version = "0.64.0"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.19"
|
||||
itertools = "0.10"
|
||||
miette = "4.5.0"
|
||||
thiserror = "1.0.29"
|
||||
serde_json = "1.0"
|
||||
|
@ -22,6 +22,7 @@ use crate::parse_keywords::{
|
||||
parse_use,
|
||||
};
|
||||
|
||||
use itertools::Itertools;
|
||||
use log::trace;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
@ -1206,9 +1207,13 @@ fn parse_binary_with_base(
|
||||
}
|
||||
|
||||
fn decode_with_base(s: &str, base: u32, digits_per_byte: usize) -> Result<Vec<u8>, ParseIntError> {
|
||||
(0..s.len())
|
||||
.step_by(digits_per_byte)
|
||||
.map(|i| u8::from_str_radix(&s[i..i + digits_per_byte], base))
|
||||
s.chars()
|
||||
.chunks(digits_per_byte)
|
||||
.into_iter()
|
||||
.map(|chunk| {
|
||||
let str: String = chunk.collect();
|
||||
u8::from_str_radix(&str, base)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
@ -159,6 +159,22 @@ pub fn parse_binary_with_invalid_octal_format() {
|
||||
assert!(!matches!(&expressions[0].expr, Expr::Binary(_)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_binary_with_multi_byte_char() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
// found using fuzzing, Rust can panic if you slice into this string
|
||||
let contents = b"0x[\xEF\xBF\xBD]";
|
||||
let (block, err) = parse(&mut working_set, None, contents, true, &[]);
|
||||
|
||||
assert!(err.is_none());
|
||||
assert!(block.len() == 1);
|
||||
let expressions = &block[0];
|
||||
assert!(expressions.len() == 1);
|
||||
assert!(!matches!(&expressions[0].expr, Expr::Binary(_)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_string() {
|
||||
let engine_state = EngineState::new();
|
||||
|
Loading…
Reference in New Issue
Block a user