Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit df3530d

Browse filesBrowse files
committed
Merge pull request #3845 from mbyt/out_of_memory_access_as_strided
BUG: ensure integer shape, strides for as_strided
2 parents 1342d32 + 81492ad commit df3530d
Copy full SHA for df3530d

File tree

Expand file treeCollapse file tree

2 files changed

+27
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+27
-0
lines changed

‎lib/matplotlib/mlab.py

Copy file name to clipboardExpand all lines: lib/matplotlib/mlab.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ def stride_windows(x, n, noverlap=None, axis=0):
590590
if n > x.size:
591591
raise ValueError('n cannot be greater than the length of x')
592592

593+
# np.lib.stride_tricks.as_strided easily leads to memory corruption for
594+
# non integer shape and strides, i.e. noverlap or n. See #3845.
595+
noverlap = int(noverlap)
596+
n = int(n)
597+
593598
step = n - noverlap
594599
if axis == 0:
595600
shape = (n, (x.shape[-1]-noverlap)//step)
@@ -642,6 +647,10 @@ def stride_repeat(x, n, axis=0):
642647
if n < 1:
643648
raise ValueError('n cannot be less than 1')
644649

650+
# np.lib.stride_tricks.as_strided easily leads to memory corruption for
651+
# non integer shape and strides, i.e. n. See #3845.
652+
n = int(n)
653+
645654
if axis == 0:
646655
shape = (n, x.size)
647656
strides = (0, x.strides[0])

‎lib/matplotlib/tests/test_mlab.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_mlab.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,24 @@ def test_stride_windows_n32_noverlap0_axis1_unflatten(self):
283283
assert_equal(y.shape, x1.shape)
284284
assert_array_equal(y, x1)
285285

286+
def test_stride_ensure_integer_type(self):
287+
N = 100
288+
x = np.empty(N + 20, dtype='>f4')
289+
x.fill(np.NaN)
290+
y = x[10:-10]
291+
y.fill(0.3)
292+
# previous to #3845 lead to corrupt access
293+
y_strided = mlab.stride_windows(y, n=33, noverlap=0.6)
294+
assert_array_equal(y_strided, 0.3)
295+
# previous to #3845 lead to corrupt access
296+
y_strided = mlab.stride_windows(y, n=33.3, noverlap=0)
297+
assert_array_equal(y_strided, 0.3)
298+
# even previous to #3845 could not find any problematic
299+
# configuration however, let's be sure it's not accidentally
300+
# introduced
301+
y_strided = mlab.stride_repeat(y, n=33.815)
302+
assert_array_equal(y_strided, 0.3)
303+
286304

287305
class csv_testcase(CleanupTestCase):
288306
def setUp(self):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.