summaryrefslogtreecommitdiff
path: root/numpy/f2py/tests
diff options
context:
space:
mode:
authorSebastian Berg <sebastianb@nvidia.com>2023-04-26 10:51:21 +0200
committerGitHub <noreply@github.com>2023-04-26 10:51:21 +0200
commitb4f0e4122e83115d99de849fcfc4fd1fc8b8f65d (patch)
tree0695fcc40428a235161418f2bd288f9a90e3d640 /numpy/f2py/tests
parentc7ff4118a345c966e2a0fc688e054e3fd9191e99 (diff)
parent5665fdcdf21dec575d204852b208eaecf1a1638d (diff)
downloadnumpy-b4f0e4122e83115d99de849fcfc4fd1fc8b8f65d.tar.gz
Merge branch 'main' into f2pyFuncFix_23598
Diffstat (limited to 'numpy/f2py/tests')
-rw-r--r--numpy/f2py/tests/src/string/scalar_string.f902
-rw-r--r--numpy/f2py/tests/test_character.py22
-rw-r--r--numpy/f2py/tests/test_crackfortran.py38
3 files changed, 28 insertions, 34 deletions
diff --git a/numpy/f2py/tests/src/string/scalar_string.f90 b/numpy/f2py/tests/src/string/scalar_string.f90
index d847668bb..f8f076172 100644
--- a/numpy/f2py/tests/src/string/scalar_string.f90
+++ b/numpy/f2py/tests/src/string/scalar_string.f90
@@ -1,7 +1,9 @@
MODULE string_test
character(len=8) :: string
+ character string77 * 8
character(len=12), dimension(5,7) :: strarr
+ character strarr77(5,7) * 12
END MODULE string_test
diff --git a/numpy/f2py/tests/test_character.py b/numpy/f2py/tests/test_character.py
index 5f9805158..0bb0f4290 100644
--- a/numpy/f2py/tests/test_character.py
+++ b/numpy/f2py/tests/test_character.py
@@ -576,16 +576,18 @@ class TestStringScalarArr(util.F2PyTest):
@pytest.mark.slow
def test_char(self):
- out = self.module.string_test.string
- expected = ()
- assert out.shape == expected
- expected = '|S8'
- assert out.dtype == expected
+ for out in (self.module.string_test.string,
+ self.module.string_test.string77):
+ expected = ()
+ assert out.shape == expected
+ expected = '|S8'
+ assert out.dtype == expected
@pytest.mark.slow
def test_char_arr(self):
- out = self.module.string_test.strarr
- expected = (5,7)
- assert out.shape == expected
- expected = '|S12'
- assert out.dtype == expected
+ for out in (self.module.string_test.strarr,
+ self.module.string_test.strarr77):
+ expected = (5,7)
+ assert out.shape == expected
+ expected = '|S12'
+ assert out.dtype == expected
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):