From 8a0bd20e849cc041de5e15667893a7be89d98f68 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Sat, 23 Jul 2022 09:31:06 +0800 Subject: [PATCH] Prevents panic when parsing JSON containing large number (#6096) * prevents panic when parsing JSON containing large number * fmt * check for '-' sign first * fmt * clippy --- crates/nu-json/src/util.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/crates/nu-json/src/util.rs b/crates/nu-json/src/util.rs index 5f650b1095..0d31e06a58 100644 --- a/crates/nu-json/src/util.rs +++ b/crates/nu-json/src/util.rs @@ -207,18 +207,19 @@ impl> ParseNumber { } } - if is_float { - Ok(Number::F64( - res.parse::().expect("Internal error: json parsing"), - )) - } else if res.starts_with('-') { - Ok(Number::I64( - res.parse::().expect("Internal error: json parsing"), - )) - } else { - Ok(Number::U64( - res.parse::().expect("Internal error: json parsing"), - )) + if !is_float { + if res.starts_with('-') { + if let Ok(n) = res.parse::() { + return Ok(Number::I64(n)); + } + } else if let Ok(n) = res.parse::() { + return Ok(Number::U64(n)); + } + } + + match res.parse::() { + Ok(n) => Ok(Number::F64(n)), + _ => Err(Error::Syntax(ErrorCode::InvalidNumber, 0, 0)), } } _ => Err(Error::Syntax(ErrorCode::InvalidNumber, 0, 0)),