[Python-Dev] Segfault

Hrvoje Nikšić hrvoje.niksic at avl.com
Tue Aug 21 10:13:48 CEST 2007


On Mon, 2007-08-20 at 20:27 +0200, Maciej Fijalkowski wrote:
> import thread
> 
> while 1:
>     f = open("/tmp/dupa", "w")
>     thread.start_new_thread(f.close, ())
>     f.close()

file_close inadvertently allows fclose to be called twice on the same
stdio file.  This patch should fix the problem:

Python-2.5.1/Objects/fileobject.c
--- Python-2.5.1.orig/Objects/fileobject.c	2007-01-23 14:54:30.000000000 +0100
+++ Python-2.5.1/Objects/fileobject.c	2007-08-21 10:04:18.000000000 +0200
@@ -440,13 +440,14 @@
 {
 	int sts = 0;
 	if (f->f_fp != NULL) {
+		FILE *fp = f->f_fp;
+		f->f_fp = NULL;
 		if (f->f_close != NULL) {
 			Py_BEGIN_ALLOW_THREADS
 			errno = 0;
-			sts = (*f->f_close)(f->f_fp);
+			sts = (*f->f_close)(fp);
 			Py_END_ALLOW_THREADS
 		}
-		f->f_fp = NULL;
 	}
 	PyMem_Free(f->f_setbuf);
 	f->f_setbuf = NULL;




More information about the Python-Dev mailing list