Skip to content

[3.9] gh-96710: Make the test timing more lenient for the int/str DoS regression test. (GH-96717) #98196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ def test_denial_of_service_prevented_int_to_str(self):
self.assertEqual(len(huge_decimal), digits)
# Ensuring that we chose a slow enough conversion to measure.
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
if seconds_to_convert < 0.005:
# Some OSes have a low res 1/64s timer, skip if hard to measure.
if seconds_to_convert < 1/64:
raise unittest.SkipTest('"slow" conversion took only '
f'{seconds_to_convert} seconds.')

Expand All @@ -656,7 +657,7 @@ def test_denial_of_service_prevented_int_to_str(self):
str(huge_int)
seconds_to_fail_huge = get_time() - start
self.assertIn('conversion', str(err.exception))
self.assertLess(seconds_to_fail_huge, seconds_to_convert/8)
self.assertLessEqual(seconds_to_fail_huge, seconds_to_convert/2)

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

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

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

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

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