From 28d2459d4b65110d6e6c1e18470ce7dcb7113282 Mon Sep 17 00:00:00 2001 From: Zaahir Moolla Date: Wed, 28 Nov 2018 13:02:26 -0500 Subject: [PATCH] Factors & Prime Factors: Limit input size to <= 1M (#4669) * Limit input size to Factors IA * Limit input size of PrimeFactors Goodie --- lib/DDG/Goodie/Factors.pm | 7 ++++++- lib/DDG/Goodie/PrimeFactors.pm | 2 ++ t/Factors.t | 1 + t/PrimeFactors.t | 9 +++++---- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/DDG/Goodie/Factors.pm b/lib/DDG/Goodie/Factors.pm index 982c0694b..f565b1877 100755 --- a/lib/DDG/Goodie/Factors.pm +++ b/lib/DDG/Goodie/Factors.pm @@ -22,7 +22,12 @@ handle remainder => sub { # query number is negative, find and include negative factors. my $negative = $1; my $query_mag = $2; - my @factors = divisors($query_mag); + + # max input value of 1M + # anything big takes too long to calculate + return unless $query_mag <= 1000000; + + my @factors = divisors($query_mag); unshift @factors, sort { $a <=> $b } map { -$_ } @factors if $negative; diff --git a/lib/DDG/Goodie/PrimeFactors.pm b/lib/DDG/Goodie/PrimeFactors.pm index cbfc78c52..8201b2165 100644 --- a/lib/DDG/Goodie/PrimeFactors.pm +++ b/lib/DDG/Goodie/PrimeFactors.pm @@ -102,6 +102,8 @@ handle remainder => sub { # Extract only the number from the remainder $_ =~ s/\D+//; + return unless $_ <= 1000000; + my @factors = factor_exp($_); # Exit if we didn't find anything. diff --git a/t/Factors.t b/t/Factors.t index 92c817c2f..d49326ec3 100755 --- a/t/Factors.t +++ b/t/Factors.t @@ -56,6 +56,7 @@ ddg_goodie_test( '-12', '-12, -6, -4, -3, -2, -1, 1, 2, 3, 4, 6, 12' ), + 'factors of 999999999' => undef, 'factors of 2.4' => undef, 'factors of fear' => undef, ); diff --git a/t/PrimeFactors.t b/t/PrimeFactors.t index 0c9041d3b..8e3953e0a 100644 --- a/t/PrimeFactors.t +++ b/t/PrimeFactors.t @@ -27,9 +27,9 @@ sub build_answer { } ddg_goodie_test( - [qw( - DDG::Goodie::PrimeFactors - )], + [qw( + DDG::Goodie::PrimeFactors + )], '72 prime factors' => test_zci('The prime factorization of 72 is 2^3 × 3^2', build_answer('72 - Prime Factors', '2³ × 3²')), 'prime factors of 111' => test_zci('The prime factorization of 111 is 3 × 37', @@ -52,7 +52,8 @@ ddg_goodie_test( build_answer(undef, '83 is a prime number')), 'is 83 a prime number' => test_zci('83 is a prime number', build_answer(undef, '83 is a prime number')), - 'optimus prime 45' => undef + 'optimus prime 45' => undef, + 'prime factors of 9999999' => undef ); done_testing;