summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2001-10-16 20:39:49 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2001-10-16 20:39:49 (GMT)
commit511e2cacc4aa67a83ed7c0ae34355407bdd13c57 (patch)
tree770b92726208bcae44db86fc8956348fe8d9a91e /Lib
parent7a59445e3783fc842bc4fd0181b18d6798883a3e (diff)
downloadcpython-511e2cacc4aa67a83ed7c0ae34355407bdd13c57.zip
cpython-511e2cacc4aa67a83ed7c0ae34355407bdd13c57.tar.gz
cpython-511e2cacc4aa67a83ed7c0ae34355407bdd13c57.tar.bz2
[ #403753 ] zlib decompress; uncontrollable memory usage
Mostly by Toby Dickenson and Titus Brown. Add an optional argument to a decompression object's decompress() method. The argument specifies the maximum length of the return value. If the uncompressed data exceeds this length, the excess data is stored as the unconsumed_tail attribute. (Not to be confused with unused_data, which is a separate issue.) Difference from SF patch: Default value for unconsumed_tail is "" rather than None. It's simpler if the attribute is always a string.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/output/test_zlib3
-rw-r--r--Lib/test/test_zlib.py30
2 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/output/test_zlib b/Lib/test/output/test_zlib
index 61c33cf..1c2e2e9 100644
--- a/Lib/test/output/test_zlib
+++ b/Lib/test/output/test_zlib
@@ -8,4 +8,7 @@ normal compression/decompression succeeded
compress/decompression obj succeeded
decompress with init options succeeded
decompressobj with init options succeeded
+should be '': ''
+max_length decompressobj succeeded
+unconsumed_tail should be '': ''
Testing on 17K of random data
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 439db22..915f582 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -76,6 +76,36 @@ if decomp2 != buf:
else:
print "decompressobj with init options succeeded"
+print "should be '':", `deco.unconsumed_tail`
+
+# Check a decompression object with max_length specified
+deco = zlib.decompressobj(-12)
+cb = combuf
+bufs = []
+while cb:
+ max_length = 1 + len(cb)/10
+ chunk = deco.decompress(cb, max_length)
+ if len(chunk) > max_length:
+ print 'chunk too big (%d>%d)' % (len(chunk),max_length)
+ bufs.append(chunk)
+ cb = deco.unconsumed_tail
+bufs.append(deco.flush())
+decomp2 = ''.join(buf)
+if decomp2 != buf:
+ print "max_length decompressobj failed"
+else:
+ print "max_length decompressobj succeeded"
+
+# Misc tests of max_length
+deco = zlib.decompressobj(-12)
+try:
+ deco.decompress("", -1)
+except ValueError:
+ pass
+else:
+ print "failed to raise value error on bad max_length"
+print "unconsumed_tail should be '':", `deco.unconsumed_tail`
+
# Test flush() with the various options, using all the different levels
# in order to provide more variations.
sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']