From cc04b9a916a618bb7539d76a651b955647bfa4f5 Mon Sep 17 00:00:00 2001 From: mike <98623181+1Kinoti@users.noreply.github.com> Date: Sun, 28 May 2023 13:56:58 +0300 Subject: [PATCH] fix conflict between filesize and hexadecimal numbers (#9309) closes #9278 # Description removes ambiguity between the `b` the filesize `bytes` unit and `b` the hex digit --- crates/nu-parser/src/parser.rs | 6 ++++++ src/tests/test_parser.rs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 1446e6e97e..a9e63c5318 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2217,6 +2217,12 @@ pub fn parse_filesize(working_set: &mut StateWorkingSet, span: Span) -> Expressi let bytes = working_set.get_span_contents(span); + // the hex digit `b` might be mistaken for the unit `b`, so check that first + if bytes.starts_with(b"0x") { + working_set.error(ParseError::Expected("filesize with valid units", span)); + return garbage(span); + } + match parse_unit_value(bytes, span, FILESIZE_UNIT_GROUPS, Type::Filesize, |x| { x.to_uppercase() }) { diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index a473d9d4dc..510c4220f0 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -561,3 +561,8 @@ fn filesize_with_underscores_2() -> TestResult { fn filesize_with_underscores_3() -> TestResult { fail_test("42m_b", "executable was not found") } + +#[test] +fn filesize_is_not_hex() -> TestResult { + run_test("0x42b", "1067") +}