This is the mail archive of the
newlib@sources.redhat.com
mailing list for the newlib project.
[PATCH]: File pointer release
- From: Thomas Pfaff <tpfaff at gmx dot net>
- To: newlib at sources dot redhat dot com
- Date: Mon, 19 Jan 2004 21:39:35 +0100
- Subject: [PATCH]: File pointer release
The FILE pointer release in fclose and freopen is not thread safe.
As soon as the FILE pointers _flag member is set to 0 the FILE pointer
can be reused by other threads. This can be avoided by resetting the
_flag member as the final operation.
Thomas
2004-01-19 Thomas Pfaff <tpfaff@gmx.net>
* libc/stdio/fclose.c (fclose): Release FILE as the last step.
* libc/stdio/freopen.c (freopen): Ditto.
diff -urp libc.org/stdio/fclose.c libc/stdio/fclose.c
--- libc.org/stdio/fclose.c 2004-01-19 09:44:42.879416000 +0100
+++ libc/stdio/fclose.c 2004-01-19 09:46:39.256758400 +0100
@@ -83,11 +83,11 @@ _DEFUN (fclose, (fp),
FREEUB (fp);
if (HASLB (fp))
FREELB (fp);
- fp->_flags = 0; /* release this FILE for reuse */
_funlockfile(fp);
#ifndef __SINGLE_THREAD__
__lock_close_recursive (*(_LOCK_RECURSIVE_T *)&fp->_lock);
#endif
+ fp->_flags = 0; /* release this FILE for reuse */
return (r);
}
diff -urp libc.org/stdio/freopen.c libc/stdio/freopen.c
--- libc.org/stdio/freopen.c 2004-01-19 09:45:11.911161600 +0100
+++ libc/stdio/freopen.c 2004-01-19 09:46:19.959009600 +0100
@@ -148,12 +148,12 @@ _DEFUN (_freopen_r, (ptr, file, mode, fp
if (f < 0)
{ /* did not get it after all */
- fp->_flags = 0; /* set it free */
ptr->_errno = e; /* restore in case _close clobbered */
_funlockfile(fp);
#ifndef __SINGLE_THREAD__
__lock_close_recursive (*(_LOCK_RECURSIVE_T *)&fp->_lock);
#endif
+ fp->_flags = 0; /* set it free */
return NULL;
}