classification
Title: ZipFile doesn't range check in _EndRecData()
Type: Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: mcherm Nosy List: alanmcintyre, ebfe, mcherm, neologix, ymgve
Priority: normal Keywords: patch

Created on 2009-01-05 15:24 by ymgve, last changed 2010-09-19 21:59 by alanmcintyre.

Files
File name Uploaded Description Edit
64times01-double.zip ymgve, 2009-01-05 15:36
issue4844.diff gpolo, 2009-01-05 21:05 review
issue4844-with-test.diff alanmcintyre, 2010-08-22 01:39 added test to patch review
Messages (6)
msg79155 - (view) Author: Yngve AAdlandsvik (ymgve) Date: 2009-01-05 15:24
If you have a .zip file with an incomplete "End of Central Directory" 
record, _EndRecData() will throw a struct.error:

D:\c64workdir\Ultimate_Mag_Archive>e:ziptest.py "old - 
Ultimate_Mag_Archive"
Handling A-z\0\64times01-double.zip
Traceback (most recent call last):
  File "E:\wwwroot\c64db\tools\ziptest.py", line 48, in <module>
    ok = handle_file(data, rel_filename)
  File "E:\wwwroot\c64db\tools\ziptest.py", line 19, in handle_file
    z = zipfile.ZipFile(cStringIO.StringIO(data), "r")
  File "C:\Python26\lib\zipfile.py", line 698, in __init__
    self._GetContents()
  File "C:\Python26\lib\zipfile.py", line 718, in _GetContents
    self._RealGetContents()
  File "C:\Python26\lib\zipfile.py", line 728, in _RealGetContents
    endrec = _EndRecData(fp)
  File "C:\Python26\lib\zipfile.py", line 219, in _EndRecData
    endrec = list(struct.unpack(structEndArchive, recData))
struct.error: unpack requires a string argument of length 22

The fix is to include a check to see if there is data enough for the 
whole record before attempting to unpack.
msg79156 - (view) Author: Lukas Lueg (ebfe) Date: 2009-01-05 15:28
please attach 64times01-double.zip if possible
msg79158 - (view) Author: Yngve AAdlandsvik (ymgve) Date: 2009-01-05 15:36
Here is the file. Note that this can be reproduced with any zip file if 
you delete the last byte of the file.
msg114636 - (view) Author: Alan McIntyre (alanmcintyre) (Python committer) Date: 2010-08-22 01:39
I wrote a test for this and tried out the patch on the Python3 trunk, and it seems to work ok.  I've attached an updated patch that includes the test.

It probably wouldn't hurt to go look for other places where a struct is being unpacked without checking lengths first, and see if it makes sense to add a similar check in those places, too.  I may do that later if I have some more free time.
msg116885 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2010-09-19 21:06
Following EAFP principle, it would be better - cleaner and more efficient - to put the stuct.unpack inside a try/except clause than checking the lengths beforehand.
msg116889 - (view) Author: Alan McIntyre (alanmcintyre) (Python committer) Date: 2010-09-19 21:59
I had to look up the abbreviation (Easier to Ask Forgiveness than Permission), but that does sound like a good idea.  Thanks for mentioning it. :-)
History
Date User Action Args
2010-09-19 21:59:23alanmcintyresetmessages: + msg116889
2010-09-19 21:06:09neologixsetnosy: + neologix
messages: + msg116885
2010-08-22 01:39:50alanmcintyresetfiles: + issue4844-with-test.diff

messages: + msg114636
2010-08-21 23:43:10georg.brandlsetassignee: mcherm

nosy: + alanmcintyre, mcherm
2009-01-05 21:05:22gpolosetfiles: + issue4844.diff
keywords: + patch
2009-01-05 15:36:03ymgvesetfiles: + 64times01-double.zip
messages: + msg79158
2009-01-05 15:28:17ebfesetnosy: + ebfe
messages: + msg79156
2009-01-05 15:24:10ymgvecreate