Issue 34904: Crash in ZipFile.close() when writing zip file to /dev/null (original) (raw)

Created on 2018-10-05 10:06 by erik.bray, last changed 2022-04-11 14:59 by admin.

Messages (7)
msg327118 - (view) Author: Erik Bray (erik.bray) * (Python triager) Date: 2018-10-05 10:06
Not that there is any great reason to write a zip file to /dev/null, but I had some code that happened to do so which worked on Python 2.7, but at some point this broke: Python 3.8.0a0 (heads/master:fc7d1b3, Oct 5 2018, 09:49:57) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import zipfile >>> f = zipfile.ZipFile('/dev/null', 'w') >>> f.writestr('foo.txt', 'testtesttesttesttest') >>> f.close() Traceback (most recent call last): File "", line 1, in File "/home/embray/src/python/cpython/Lib/zipfile.py", line 1813, in close self._write_end_record() File "/home/embray/src/python/cpython/Lib/zipfile.py", line 1914, in _write_end_record endrec = struct.pack(structEndArchive, stringEndArchive, struct.error: argument out of range
msg327124 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-05 11:23
Is this specific to Linux? I can reproduce this on master branch on Ubuntu but there is no error on Mac OS with the master branch.
msg327128 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-05 11:51
This may be a Linux bug. The /dev/null file is seekable, but seek() doesn't work correctly for it. It is especially confusing for buffered files. seek() always returns 0 and reset the file position. >>> f = open('/dev/null', 'wb') >>> f.seekable() True >>> f.tell() 0 >>> f.write(b'abcdefgh') 8 >>> f.tell() 8 >>> f.seek(8) 0 >>> f.tell() 0 In contrary, files like /dev/stdout are not seekable, and writing a ZIP file to them works properly.
msg327130 - (view) Author: Erik Bray (erik.bray) * (Python triager) Date: 2018-10-05 12:10
The regression was introduced by . It does seem to be Linux-specific with seek/tell on /dev/null. For example, I cannot reproduce the issue on Cygwin.
msg327131 - (view) Author: Erik Bray (erik.bray) * (Python triager) Date: 2018-10-05 12:13
On Cygwin the same tests give >>> f = open('/dev/null', 'wb') >>> f.seekable() True >>> f.write(b'abcdefgh') 8 >>> f.tell() 8 >>> f.seek(8) 8 >>> f.tell() 8 I would also try macOS if I could. But yes, I wonder if it's a Linux bug.
msg327132 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-05 12:21
Thanks Erik for the details. On Mac with master also it works like Cygwin. ./python.exe Python 3.8.0a0 (heads/master:6f85b826b5, Oct 4 2018, 22:44:36) [Clang 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> f = open('/dev/null', 'wb') >>> f.seekable() True >>> f.write(b'abcdefgh') 8 >>> f.tell() 8 >>> f.seek(8) 8 >>> f.tell() 8
msg327135 - (view) Author: Erik Bray (erik.bray) * (Python triager) Date: 2018-10-05 13:12
For the sake of completeness, same deal in pure C: $ cat devnul.c #include <stdio.h> int main(void) { FILE *f = fopen("/dev/null", "w"); printf("tell() = %ld\n", ftell(f)); printf("write(\"abcdefgh\") = %zu\n", fwrite("abcdefgh", 1, 8, f)); printf("tell() = %ld\n", ftell(f)); printf("seek(8) = %d\n", fseek(f, 8, 0)); printf("tell() = %ld\n", ftell(f)); return 0; } $ gcc devnulc -o devnul $ ./devnull tell() = 0 write("abcdefgh") = 8 tell() = 8 seek(8) = 0 tell() = 0
History
Date User Action Args
2022-04-11 14:59:06 admin set github: 79085
2018-10-05 13:12:57 erik.bray set messages: +
2018-10-05 12:21:16 xtreak set messages: +
2018-10-05 12:13:31 erik.bray set messages: +
2018-10-05 12:10:34 erik.bray set messages: +
2018-10-05 11:51:38 serhiy.storchaka set nosy: + serhiy.storchakamessages: + components: + IOtype: crash -> behavior
2018-10-05 11:23:37 xtreak set nosy: + xtreakmessages: +
2018-10-05 10:06:44 erik.bray create