mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 13:01:10 +02:00
Allocation of a large compression window may fail, and in that case keep the `DeflateIO` state consistent so its other methods (such as `close()`) still work. Consistency is kept by only updating the `self->write` member if the window allocation succeeds. Thanks to @jimmo for finding the bug. Signed-off-by: Damien George <damien@micropython.org>
40 lines
809 B
Python
40 lines
809 B
Python
# Test deflate.DeflateIO compression, with out-of-memory errors.
|
|
|
|
try:
|
|
# Check if deflate is available.
|
|
import deflate
|
|
import io
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
# Check if compression is enabled.
|
|
if not hasattr(deflate.DeflateIO, "write"):
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
# Create a compressor object.
|
|
b = io.BytesIO()
|
|
g = deflate.DeflateIO(b, deflate.RAW, 15)
|
|
|
|
# Then, use up most of the heap.
|
|
l = []
|
|
while True:
|
|
try:
|
|
l.append(bytearray(1000))
|
|
except:
|
|
break
|
|
l.pop()
|
|
|
|
# Try to compress. This will try to allocate a large window and fail.
|
|
try:
|
|
g.write('test')
|
|
except MemoryError:
|
|
print("MemoryError")
|
|
|
|
# Should still be able to close the stream.
|
|
g.close()
|
|
|
|
# The underlying output stream should be unchanged.
|
|
print(b.getvalue())
|