Skip to content

Instantly share code, notes, and snippets.

@Mulugruntz
Created February 28, 2020 08:24
Show Gist options
  • Save Mulugruntz/2c668a70c50f8272bce0c208aaae924e to your computer and use it in GitHub Desktop.
Save Mulugruntz/2c668a70c50f8272bce0c208aaae924e to your computer and use it in GitHub Desktop.
ndarray gets filled with or without the buffer being specified. It seems to read some memory it shouldn't have access to?
import numpy
val = numpy.ascontiguousarray(
[
[
[[4, 8, 15], [16, 23, 42], [4, 8, 15], [16, 23, 42], [4, 8, 15]],
[[4, 8, 15], [16, 23, 42], [4, 8, 15], [16, 23, 42], [4, 8, 15]],
[[4, 8, 15], [16, 23, 42], [4, 8, 15], [16, 23, 42], [4, 8, 15]],
[[4, 8, 15], [16, 23, 42], [4, 8, 15], [16, 23, 42], [4, 8, 15]],
[[4, 8, 15], [16, 23, 42], [4, 8, 15], [16, 23, 42], [4, 8, 15]],
]
],
dtype="uint8",
)
frame = numpy.ndarray((int(5), int(5), 3), dtype="uint8", buffer=val)
data = bytes(frame)
del frame
del val
frame2 = numpy.ndarray((int(5), int(5), 3), dtype="uint8", buffer=data)
frame3 = numpy.ndarray((int(5), int(5), 3), dtype="uint8")
assert numpy.array_equal(frame2, frame3)
@Mulugruntz
Copy link
Author

Tested with Python 3.8.1 and numpy==1.18.1 on MacOS Mojave 10.14.5.
Assert is not triggered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment