[issue6390] File reads past EOF in "w+b" mode

Brian Mearns report at bugs.python.org
Tue Jun 30 18:52:50 CEST 2009


New submission from Brian Mearns <bmearns at ieee.org>:

Open a file in "w+b" mode: if you write to the file, then read from it
without seeking backward, it reads past the EOF, apparently out into
memory, which could be a pretty bad security concern. Have not checked
if "w+" mode does the same.

### Bad behavior...
>>> fid = open("temp", "w+b")
>>> fid.read()
''
>>> fid.write("foobar")
#Read while positioned on EOF
>>> fid.read(10)
'\xc2\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> fid.seek(0)
>>> fid.read(10)
'foobar\xc2\x00\x00\x00'
>>> fid.close()

###Correct behavior after seeking backwards:
>>> fid = open("temp2", "w+b")
>>> fid.read()
''
>>> fid.write("foobar")
>>> fid.seek(0)
>>> fid.read(10)
'foobar'
>>> fid.close()

Interestingly, it appears that any seek works, you don't necessarily
have to go backwards:

>>> fid = open("temp2", "w+b")
>>> fid.write("foobar")
>>> fid.tell()
6L
>>> fid.seek(6)
>>> fid.read()
''

----------
components: IO
messages: 89941
nosy: bmearns
severity: normal
status: open
title: File reads past EOF in "w+b" mode
type: security
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6390>
_______________________________________


More information about the Python-bugs-list mailing list