]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport to 2.3:
authorAndrew MacIntyre <andymac@bullseye.apana.org.au>
Sun, 4 Apr 2004 07:08:20 +0000 (07:08 +0000)
committerAndrew MacIntyre <andymac@bullseye.apana.org.au>
Sun, 4 Apr 2004 07:08:20 +0000 (07:08 +0000)
If a file is opened with an explicit buffer size >= 1, repeated
close() calls would attempt to free() the buffer already free()ed on
the first close().     [bug introduced with patch #788249]

Making sure that the buffer is free()ed in file object deallocation is
a belt-n-braces bit of insurance against a memory leak.

Lib/test/test_file.py
Misc/NEWS
Objects/fileobject.c

index 677dafc3f0dfa95a1d430f098c831e0cdafdfadf..62b0e669a4eae13d7ab9489dc8a9292966266f62 100644 (file)
@@ -109,6 +109,23 @@ f.close()
 if not f.closed:
     raise TestFailed, 'file.closed should be true'
 
+# make sure that explicitly setting the buffer size doesn't cause
+# misbehaviour especially with repeated close() calls
+for s in (-1, 0, 1, 512):
+    try:
+        f = open(TESTFN, 'w', s)
+        f.write(str(s))
+        f.close()
+        f.close()
+        f = open(TESTFN, 'r', s)
+        d = int(f.read())
+        f.close()
+        f.close()
+    except IOError, msg:
+        raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg))
+    if d != s:
+        raise TestFailed, 'readback failure using buffer size %d'
+
 methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
            'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
            'xreadlines', '__iter__']
index bacc90991e50e88d8037b793d2c6677a51596e73..7d6297c0ee6b8422789707f09e875ed0658123d2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,10 @@ Core and builtins
   to clear the error when attempts to get the __getstate__ attribute
   fail caused intermittent errors and odd behavior.
 
+- fixed: if a file is opened with an explicit buffer size >= 1, repeated
+  close() calls would attempt to free() the buffer already free()ed on
+  the first call.
+
 Library
 -------
 
index 127abceae41ea4d960ba61e97b2be8cd7447cb2c..11bbeb25978c9a1927785b325e83c738939249f8 100644 (file)
@@ -352,6 +352,7 @@ file_dealloc(PyFileObject *f)
                (*f->f_close)(f->f_fp);
                Py_END_ALLOW_THREADS
        }
+       PyMem_Free(f->f_setbuf);
        Py_XDECREF(f->f_name);
        Py_XDECREF(f->f_mode);
        Py_XDECREF(f->f_encoding);
@@ -398,6 +399,7 @@ file_close(PyFileObject *f)
                f->f_fp = NULL;
        }
        PyMem_Free(f->f_setbuf);
+       f->f_setbuf = NULL;
        if (sts == EOF)
                return PyErr_SetFromErrno(PyExc_IOError);
        if (sts != 0)