Skip to content

Commit 5a17200

Browse files
gh-96710: Make the test timing more lenient for the int/str DoS regression test. (GH-96717)
A regression would still absolutely fail and even a flaky pass isn't harmful as it'd fail most of the time across our N system test runs. Windows has a low resolution timer and CI systems are prone to odd timing so this just gives more leeway to avoid flakiness. (cherry picked from commit 11e3548) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent f60bbf0 commit 5a17200

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Lib/test/test_int.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ def test_denial_of_service_prevented_int_to_str(self):
641641
self.assertEqual(len(huge_decimal), digits)
642642
# Ensuring that we chose a slow enough conversion to measure.
643643
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
644-
if seconds_to_convert < 0.005:
644+
# Some OSes have a low res 1/64s timer, skip if hard to measure.
645+
if seconds_to_convert < 1/64:
645646
raise unittest.SkipTest('"slow" conversion took only '
646647
f'{seconds_to_convert} seconds.')
647648

@@ -653,7 +654,7 @@ def test_denial_of_service_prevented_int_to_str(self):
653654
str(huge_int)
654655
seconds_to_fail_huge = get_time() - start
655656
self.assertIn('conversion', str(err.exception))
656-
self.assertLess(seconds_to_fail_huge, seconds_to_convert/8)
657+
self.assertLessEqual(seconds_to_fail_huge, seconds_to_convert/2)
657658

658659
# Now we test that a conversion that would take 30x as long also fails
659660
# in a similarly fast fashion.
@@ -664,7 +665,7 @@ def test_denial_of_service_prevented_int_to_str(self):
664665
str(extra_huge_int)
665666
seconds_to_fail_extra_huge = get_time() - start
666667
self.assertIn('conversion', str(err.exception))
667-
self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8)
668+
self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/2)
668669

669670
def test_denial_of_service_prevented_str_to_int(self):
670671
"""Regression test: ensure we fail before performing O(N**2) work."""
@@ -682,7 +683,8 @@ def test_denial_of_service_prevented_str_to_int(self):
682683
seconds_to_convert = get_time() - start
683684
# Ensuring that we chose a slow enough conversion to measure.
684685
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
685-
if seconds_to_convert < 0.005:
686+
# Some OSes have a low res 1/64s timer, skip if hard to measure.
687+
if seconds_to_convert < 1/64:
686688
raise unittest.SkipTest('"slow" conversion took only '
687689
f'{seconds_to_convert} seconds.')
688690

@@ -692,7 +694,7 @@ def test_denial_of_service_prevented_str_to_int(self):
692694
int(huge)
693695
seconds_to_fail_huge = get_time() - start
694696
self.assertIn('conversion', str(err.exception))
695-
self.assertLess(seconds_to_fail_huge, seconds_to_convert/8)
697+
self.assertLessEqual(seconds_to_fail_huge, seconds_to_convert/2)
696698

697699
# Now we test that a conversion that would take 30x as long also fails
698700
# in a similarly fast fashion.
@@ -703,7 +705,7 @@ def test_denial_of_service_prevented_str_to_int(self):
703705
int(extra_huge)
704706
seconds_to_fail_extra_huge = get_time() - start
705707
self.assertIn('conversion', str(err.exception))
706-
self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8)
708+
self.assertLessEqual(seconds_to_fail_extra_huge, seconds_to_convert/2)
707709

708710
def test_power_of_two_bases_unlimited(self):
709711
"""The limit does not apply to power of 2 bases."""

0 commit comments

Comments
 (0)