This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: file_close() ignores return value of close_the_file
Type: crash Stage: resolved
Components: Interpreter Core, IO Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: stutzbach Nosy List: gregory.p.smith, pitrou, stutzbach, tim.peters
Priority: high Keywords: needs review, patch

Created on 2009-10-07 21:10 by stutzbach, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
file_close_test.patch stutzbach, 2010-04-29 16:54 Patch to add a test case that crashes Python 2.6 and trunk
file_close.patch stutzbach, 2010-04-29 16:55 Patch to fix the bug
Messages (5)
msg93723 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-10-07 21:10
I noticed that file_close() calls close_the_file(), then frees the
buffer for the file object.  However, close_the_file() may fail and
return NULL if the file object is currently in use by another thread, in
which case freeing the buffer from underneath the C stdio library may
cause a crash.

Here's the relevant bit of code from fileobject.c:

static PyObject *
file_close(PyFileObject *f)
{
        PyObject *sts = close_the_file(f);
        PyMem_Free(f->f_setbuf);
        f->f_setbuf = NULL;
        return sts;
}

I think the two middle lines of the function should be wrapped in an "if
(sts)" block.

Attached is a short program that causes python to crash on two of my
systems (Windows XP running Python 2.6.3 and Debian running Python 2.5)
and a patch with my proposed fix.

I think Python 3 is immune because the I/O code has been completely
rewritten.  I have not checked the Python 3 code to see if there are any
analogous problems in the new code, however.
msg104353 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-27 20:10
Your proposal looks reasonable.
Two things:
- your patch should include an unit test (see Lib/test/test_file2k.py)
- fileobject.c should use tabs for indentation, not spaces

And you're right, py3k doesn't have this problem.
msg104539 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-04-29 16:57
I reworked my test script into a unit test.  Thanks for pointing me to test_file2k.py.  I leveraged the infrastructure that was already there.

I've also uploaded a new patch that uses tabs instead of spaces, to match the rest of fileobject.c.
msg105932 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-05-17 19:11
Is there anything more I can do to help get this crash-fix committed before 2.7 rc1?
msg105933 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-05-17 20:01
Sorry, I had just forgotten. The patch has been committed in r81275 (trunk) and r81277 (2.6). Thank you:
History
Date User Action Args
2022-04-11 14:56:53adminsetgithub: 51328
2010-05-17 20:01:29pitrousetstatus: open -> closed
resolution: fixed
messages: + msg105933

stage: patch review -> resolved
2010-05-17 19:11:45stutzbachsetmessages: + msg105932
2010-04-29 16:57:07stutzbachsetmessages: + msg104539
stage: test needed -> patch review
2010-04-29 16:55:08stutzbachsetfiles: + file_close.patch
2010-04-29 16:54:48stutzbachsetfiles: + file_close_test.patch
2010-04-29 16:53:22stutzbachsetfiles: - crash.py
2010-04-29 16:53:17stutzbachsetfiles: - fileobject.diff
2010-04-27 20:10:16pitrousetnosy: + pitrou, tim.peters

messages: + msg104353
versions: - Python 2.5
2010-04-27 15:37:44stutzbachsetassignee: gregory.p.smith -> stutzbach
stage: patch review -> test needed
2010-04-27 15:37:09stutzbachsetkeywords: + needs review
stage: patch review
2009-10-08 07:12:16gregory.p.smithsetpriority: high
assignee: gregory.p.smith
2009-10-08 01:38:32benjamin.petersonsetnosy: + gregory.p.smith
2009-10-07 21:11:21stutzbachsetfiles: + crash.py
2009-10-07 21:10:44stutzbachcreate