From a236f85e9d0a96602fb48abd1829883b63506021 Mon Sep 17 00:00:00 2001 From: Louis DISPA Date: Thu, 13 Mar 2025 00:00:29 +0100 Subject: [PATCH] expr: Add a long input test Test a stack overflow that was happening on linux for long inputs. --- tests/by-util/test_expr.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 3fcb703f9..c391565e4 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -374,6 +374,30 @@ fn test_eager_evaluation() { .stderr_contains("division by zero"); } +#[test] +fn test_long_input() { + // Giving expr an arbitrary long expression should succeed rather than end with a segfault due to a stack overflow. + #[cfg(not(windows))] + const MAX_NUMBER: usize = 40000; + #[cfg(not(windows))] + const RESULT: &str = "800020000\n"; + + // On windows there is 8192 characters input limit + #[cfg(windows)] + const MAX_NUMBER: usize = 1300; // 7993 characters (with spaces) + #[cfg(windows)] + const RESULT: &str = "845650\n"; + + let mut args: Vec = vec!["1".to_string()]; + + for i in 2..=MAX_NUMBER { + args.push('+'.to_string()); + args.push(i.to_string()); + } + + new_ucmd!().args(&args).succeeds().stdout_is(RESULT); +} + /// Regroup the testcases of the GNU test expr.pl mod gnu_expr { use crate::common::util::TestScenario;