-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
BUG: non integer overlap might lead to corrupt memory access in as_strided [backport 1.4.x] #3845
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
BUG: non integer overlap might lead to corrupt memory access in as_strided [backport 1.4.x] #3845
Conversation
@@ -590,6 +590,19 @@ def stride_windows(x, n, noverlap=None, axis=0): | |||
if n > x.size: | |||
raise ValueError('n cannot be greater than the length of x') | |||
|
|||
# as_strided can lead to memory out of bound access for non | |||
# integer noverlap or n | |||
if not isinstance(noverlap, (int, long)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this catch all of possible numpy scalar types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A non-integer overlap is really an error, and not a very likely one. I think a simpler solution might be better: no deprecation, just use noverlap = int(noverlap)
to force it to be a python integer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was in the process of writing the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this catch all of possible numpy scalar types?
Good point, it probably does not catch all possible numpy scalar types.
and not a very likely one
Didn't think about that --- good point! I fully agree. I will remove the deprecation warning and directly cast to int.
d34a3f0
to
43fd6be
Compare
Alright, I updated as mentioned followed by a git rebase and push --force to remove the clutter. I also improved and simplified the test case. |
43fd6be
to
81492ad
Compare
Sorry and yet another test improvement lead to the above rebase 81492ad. |
No worries. Pending travis passing this looks good to me. |
BUG: ensure integer shape, strides for as_strided
BUG: ensure integer shape, strides for as_strided
back ported as edd2a87 |
That was fast! Thanks and keep up the good work! |
Just for documentation, the problem were non integer overlaps, which lead to strides, whose size was smaller than the actual data type (i 7FF4 n the example below, 1byte stride, for a 4byte float).
I clarified the title correspondingly. |
Running mlab.psd with non integer
noverlap
might lead to corrupt memory access in the underlyingnp.lib.stride_tricks.as_strides
. This is not obvious to debug, especially whennoverlap
is calculated programmatic.Example:
This was not problematic with matplotlib 0.13, as the mlab.psd implementation was not using
np.lib.stride_tricks.as_strides
at that time.