From e34c756b945fd7970875fd42e81e636fe5dbd247 Mon Sep 17 00:00:00 2001 From: solar Date: Sat, 20 Nov 2010 06:01:42 +0000 Subject: [PATCH] Bug #39: fclose( stdout ) corrupts memory --- functions/stdio/fclose.c | 6 ++++-- internals/_PDCLIB_int.h | 8 ++++++++ platform/example/functions/_PDCLIB/stdinit.c | 6 +++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/functions/stdio/fclose.c b/functions/stdio/fclose.c index 2b68694..5c1e4d4 100644 --- a/functions/stdio/fclose.c +++ b/functions/stdio/fclose.c @@ -49,8 +49,10 @@ int fclose( struct _PDCLIB_file_t * stream ) remove( stream->filename ); } /* Free stream */ - free( stream ); - + if ( ! stream->status & _PDCLIB_STATIC ) + { + free( stream ); + } return 0; } previous = current; diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index a4cf38b..0bad643 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -268,12 +268,20 @@ typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t; #define _PDCLIB_FBIN 128u /* Internal flags, made to fit the same status field as the flags above. */ +/* -------------------------------------------------------------------------- */ +/* free() the buffer memory on closing (false for user-supplied buffer) */ #define _PDCLIB_FREEBUFFER 512u +/* stream has encountered error / EOF */ #define _PDCLIB_ERRORFLAG 1024u #define _PDCLIB_EOFFLAG 2048u +/* stream is wide-oriented */ #define _PDCLIB_WIDESTREAM 4096u +/* stream is byte-oriented */ #define _PDCLIB_BYTESTREAM 8192u +/* file associated with stream should be remove()d on closing (tmpfile()) */ #define _PDCLIB_DELONCLOSE 16384u +/* stream handle should not be free()d on close (stdin, stdout, stderr) */ +#define _PDCLIB_STATIC 32768u /* Position / status structure for getpos() / fsetpos(). */ struct _PDCLIB_fpos_t diff --git a/platform/example/functions/_PDCLIB/stdinit.c b/platform/example/functions/_PDCLIB/stdinit.c index 78c76e2..8ddb774 100644 --- a/platform/example/functions/_PDCLIB/stdinit.c +++ b/platform/example/functions/_PDCLIB/stdinit.c @@ -27,9 +27,9 @@ static unsigned char _PDCLIB_sin_ungetbuf[_PDCLIB_UNGETCBUFSIZE]; static unsigned char _PDCLIB_sout_ungetbuf[_PDCLIB_UNGETCBUFSIZE]; static unsigned char _PDCLIB_serr_ungetbuf[_PDCLIB_UNGETCBUFSIZE]; -static struct _PDCLIB_file_t _PDCLIB_serr = { 2, _PDCLIB_serr_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_serr_ungetbuf, _IONBF | _PDCLIB_FWRITE, NULL, NULL }; -static struct _PDCLIB_file_t _PDCLIB_sout = { 1, _PDCLIB_sout_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_sout_ungetbuf, _IOLBF | _PDCLIB_FWRITE, NULL, &_PDCLIB_serr }; -static struct _PDCLIB_file_t _PDCLIB_sin = { 0, _PDCLIB_sin_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_sin_ungetbuf, _IOLBF | _PDCLIB_FREAD, NULL, &_PDCLIB_sout }; +static struct _PDCLIB_file_t _PDCLIB_serr = { 2, _PDCLIB_serr_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_serr_ungetbuf, _IONBF | _PDCLIB_FWRITE | _PDCLIB_STATIC, NULL, NULL }; +static struct _PDCLIB_file_t _PDCLIB_sout = { 1, _PDCLIB_sout_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_sout_ungetbuf, _IOLBF | _PDCLIB_FWRITE | _PDCLIB_STATIC, NULL, &_PDCLIB_serr }; +static struct _PDCLIB_file_t _PDCLIB_sin = { 0, _PDCLIB_sin_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_sin_ungetbuf, _IOLBF | _PDCLIB_FREAD | _PDCLIB_STATIC, NULL, &_PDCLIB_sout }; struct _PDCLIB_file_t * stdin = &_PDCLIB_sin; struct _PDCLIB_file_t * stdout = &_PDCLIB_sout; -- 2.40.0