Repository navigation
gh-163190: Do not decompress a zipfile member past its file_size in read() - #163191
hyeonsang010716 wants to merge 1 commit into
Conversation
|
One additional regression case could combine a partial first read with CRC failure. The current test corrects the CRC to the truncated prefix, which is useful for isolating the allocation bound; keeping a wrong-CRC variant would also protect the existing failure behavior. I used a bounded 64 KiB deflate member, set the declared size to 5,000, then called This was an isolated method-substitution probe for deflate on the installed interpreter, not a full PR build, other-codec test or memory benchmark. AI-assisted analysis; the bounded probe and both CRC controls were executed. |
ZipExtFile.read()without a size calls_read1(self.MAX_N), and_read1()passed that on to the decompressor asmax_lengthbefore cutting the result toself._left. A member whose central directory entry states a smallerfile_sizethan its data expands to was decompressed whole, up to 2 GiB per call, to return those few bytes. The script in the issue reads 100 bytes from a 510 KB archive and peaks at 697 MiB._read1()now asks the decompressor for no more than the member still has to give,max(min(n, self._left), self.MIN_READ_SIZE), for deflate and for bzip2, LZMA and Zstandard alike. Whatread()andread1()return is the same, and the CRC is checked over the same bytes. With the change the script peaks at 1 MiB.The test states a
file_sizeof 100 for a 4 MiB member and checks, for each compression method, thatread()andread1(-1)return those 100 bytes and that nodecompress()call returned more thanMIN_READ_SIZE. It fails without the change for the four compressed methods.Reading a 200 MiB member whole takes the same time before and after (deflate 25 ms, bzip2 330 ms, LZMA 133 ms here).
test_zipfile,test_zipimport,test_zipapp,test_shutilandtest_importlibpass.