classification
Title: file object read* methods in append mode overflows
Type: behavior Stage:
Components: Interpreter Core, Library (Lib), Windows Versions: Python 2.7
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: Otacon.Karurosu, amaury.forgeotdarc, terry.reedy
Priority: normal Keywords:

Created on 2011-08-25 23:09 by Otacon.Karurosu, last changed 2011-08-28 22:02 by amaury.forgeotdarc. This issue is now closed.

Messages (4)
msg142994 - (view) Author: Otacon Karurosu (Otacon.Karurosu) Date: 2011-08-25 23:09
When opening a file in append mode and attempting to read after doing a write operation causes the read method to overflow and read memory directly (instead of returning an empty string)

The following code prints garbage:

f2 = open("test", "w+b")
f2.write("python!")
print f2.readline()

However the following print empty lines:

f2 = open("test", "w+b")
print f2.readline()

f2 = open("test", "w+b")
f2.write("python!")
f2.seek(f2.tell())
print f2.readline()

This also happens with tempfiles:

f = tempfile.TemporaryFile()
f.write("python!")
print f.readline()
msg142999 - (view) Author: Otacon Karurosu (Otacon.Karurosu) Date: 2011-08-26 03:49
I have confirmed that this only happens in windows.
msg143118 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-08-28 18:48
> I have confirmed that this only happens in windows.

This would literally mean that you tested on several other systems. Did you actually mean 'I have only confirmed that this happens in Windows.", that you only tested on Windows?

The 2.6 series is in security-fix only mode.
msg143126 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-08-28 22:02
You should call the .flush() method when switching from writes to reads.

Nothing really overflows, but the fread() function may return uninitialized memory.  In versions 2.x, python uses the fopen, fread and fwrite function (from the C library) and is subject to their limitations.

The exact behaviour is undefined, and it is well possible that it only happens on Windows.  See also the discussion in #7952.

This issue does not exist in versions 3.x, where file functions have been rewritten.
History
Date User Action Args
2011-08-28 22:02:21amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg143126

resolution: invalid
2011-08-28 18:48:48terry.reedysetnosy: + terry.reedy

messages: + msg143118
versions: - Python 2.6
2011-08-26 03:49:02Otacon.Karurosusetmessages: + msg142999
components: + Windows
2011-08-25 23:09:02Otacon.Karurosucreate