From 2b021472d68c9473812cbc0ae222432e59bf91ce Mon Sep 17 00:00:00 2001 From: Luccas Mateus Date: Wed, 23 Jun 2021 00:44:14 -0300 Subject: [PATCH] Fixed panic on math with large durations (#3669) * Output error when ls into a file without permission * math sqrt * added test to check fails when ls into prohibited dir * fix lint * math sqrt with tests and doc * trigger wasm build * Update filesystem_shell.rs * Fix Running echo .. starts printing integers forever * Fixed panic on operations with very large durations Co-authored-by: Jonathan Turner --- crates/nu-command/tests/commands/math/mod.rs | 12 ++++++++++++ crates/nu-protocol/src/value/primitive.rs | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/tests/commands/math/mod.rs b/crates/nu-command/tests/commands/math/mod.rs index e92fd77052..21c4cab65a 100644 --- a/crates/nu-command/tests/commands/math/mod.rs +++ b/crates/nu-command/tests/commands/math/mod.rs @@ -235,6 +235,18 @@ fn duration_math_with_negative() { assert_eq!(actual.out, "-6day"); } +#[test] +fn duration_math_shell_error_on_big_numbers() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + (date now) + 100000000000000day + "# + )); + + assert!(actual.err.contains("Duration overflow")); +} + #[test] fn compound_comparison() { let actual = nu!( diff --git a/crates/nu-protocol/src/value/primitive.rs b/crates/nu-protocol/src/value/primitive.rs index 7ba2cd28e1..f325cffe4f 100644 --- a/crates/nu-protocol/src/value/primitive.rs +++ b/crates/nu-protocol/src/value/primitive.rs @@ -289,7 +289,17 @@ impl Primitive { .expect("Internal error: conversion from u32 failed"), ); let secs = match secs.to_i64() { - Some(secs) => secs, + //The duration crate doesnt accept seconds bigger than i64::MAX / 1000 + Some(secs) => match secs.checked_mul(1000) { + Some(_) => secs, + None => { + return Err(ShellError::labeled_error( + "Internal duration conversion overflow.", + "duration overflow", + span, + )) + } + }, None => { return Err(ShellError::labeled_error( "Internal duration conversion overflow.",