diff options
author | davidxu <davidxu@FreeBSD.org> | 2005-12-16 02:50:53 +0000 |
---|---|---|
committer | davidxu <davidxu@FreeBSD.org> | 2005-12-16 02:50:53 +0000 |
commit | f2547368a8bf5177a138b8873c03912af57e90af (patch) | |
tree | 6ae4ec26d50bc59ff2d9306207a3b957bc977ae6 /lib/libc/stdio/fread.c | |
parent | 1aa9ee553eae931f7025dd4707c9fd70d8789fbb (diff) | |
download | FreeBSD-src-f2547368a8bf5177a138b8873c03912af57e90af.zip FreeBSD-src-f2547368a8bf5177a138b8873c03912af57e90af.tar.gz |
With current pthread implementations, a mutex initialization will
allocate a memory block. sscanf calls __svfscanf which in turn calls
fread, fread triggers mutex initialization but the mutex is not
destroyed in sscanf, this leads to memory leak. To avoid the memory
leak and performance issue, we create a none MT-safe version of fread:
__fread, and instead let __svfscanf call __fread.
PR: threads/90392
Patch submitted by: dhartmei
MFC after: 7 days
Diffstat (limited to 'lib/libc/stdio/fread.c')
-rw-r--r-- | lib/libc/stdio/fread.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 73c7c19..3edb84a 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -47,11 +47,23 @@ __FBSDID("$FreeBSD$"); #include "local.h" #include "libc_private.h" +/* + * MT-safe version + */ + size_t -fread(buf, size, count, fp) - void * __restrict buf; - size_t size, count; - FILE * __restrict fp; +fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp) +{ + int ret; + + FLOCKFILE(fp); + ret = __fread(buf, size, count, fp); + FUNLOCKFILE(fp); + return (ret); +} + +size_t +__fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp) { size_t resid; char *p; @@ -65,7 +77,6 @@ fread(buf, size, count, fp) */ if ((resid = count * size) == 0) return (0); - FLOCKFILE(fp); ORIENT(fp, -1); if (fp->_r < 0) fp->_r = 0; @@ -79,13 +90,11 @@ fread(buf, size, count, fp) resid -= r; if (__srefill(fp)) { /* no more input: return partial result */ - FUNLOCKFILE(fp); return ((total - resid) / size); } } (void)memcpy((void *)p, (void *)fp->_p, resid); fp->_r -= resid; fp->_p += resid; - FUNLOCKFILE(fp); return (count); } |