diff options
| author | Sebastian Berg <sebastianb@nvidia.com> | 2023-04-26 10:51:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-26 10:51:21 +0200 |
| commit | b4f0e4122e83115d99de849fcfc4fd1fc8b8f65d (patch) | |
| tree | 0695fcc40428a235161418f2bd288f9a90e3d640 /numpy/f2py/tests/test_crackfortran.py | |
| parent | c7ff4118a345c966e2a0fc688e054e3fd9191e99 (diff) | |
| parent | 5665fdcdf21dec575d204852b208eaecf1a1638d (diff) | |
| download | numpy-b4f0e4122e83115d99de849fcfc4fd1fc8b8f65d.tar.gz | |
Merge branch 'main' into f2pyFuncFix_23598
Diffstat (limited to 'numpy/f2py/tests/test_crackfortran.py')
| -rw-r--r-- | numpy/f2py/tests/test_crackfortran.py | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/numpy/f2py/tests/test_crackfortran.py b/numpy/f2py/tests/test_crackfortran.py index 886fc596e..dc0f7e27a 100644 --- a/numpy/f2py/tests/test_crackfortran.py +++ b/numpy/f2py/tests/test_crackfortran.py @@ -290,39 +290,29 @@ class TestNameArgsPatternBacktracking: def test_nameargspattern_backtracking(self, adversary): '''address ReDOS vulnerability: https://github.com/numpy/numpy/issues/23338''' - last_median = 0. - trials_per_count = 128 + trials_per_batch = 12 + batches_per_regex = 4 start_reps, end_reps = 15, 25 - times_median_doubled = 0 for ii in range(start_reps, end_reps): repeated_adversary = adversary * ii - times = [] - for _ in range(trials_per_count): - t0 = time.perf_counter() - mtch = nameargspattern.search(repeated_adversary) - times.append(time.perf_counter() - t0) - # We should use a measure of time that's resilient to outliers. - # Times jump around a lot due to the CPU's scheduler. - median = np.median(times) + # test times in small batches. + # this gives us more chances to catch a bad regex + # while still catching it before too long if it is bad + for _ in range(batches_per_regex): + times = [] + for _ in range(trials_per_batch): + t0 = time.perf_counter() + mtch = nameargspattern.search(repeated_adversary) + times.append(time.perf_counter() - t0) + # our pattern should be much faster than 0.2s per search + # it's unlikely that a bad regex will pass even on fast CPUs + assert np.median(times) < 0.2 assert not mtch # if the adversary is capped with @)@, it becomes acceptable # according to the old version of the regex. # that should still be true. good_version_of_adversary = repeated_adversary + '@)@' assert nameargspattern.search(good_version_of_adversary) - if ii > start_reps: - # the hallmark of exponentially catastrophic backtracking - # is that runtime doubles for every added instance of - # the problematic pattern. - times_median_doubled += median > 2 * last_median - # also try to rule out non-exponential but still bad cases - # arbitrarily, we should set a hard limit of 10ms as too slow - assert median < trials_per_count * 0.01 - last_median = median - # we accept that maybe the median might double once, due to - # the CPU scheduler acting weird or whatever. More than that - # seems suspicious. - assert times_median_doubled < 2 class TestFunctionReturn(util.F2PyTest): |
