From a436ebdad1a4f7fb2eb75759f9e1cbf7cd876785 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Wed, 20 Oct 2010 13:12:59 +0000 Subject: [PATCH] - Reworked the __putc() and putc() macros to reference the 'c' input parameter only once, and to be free of side-effects when tinkering with the buffer position. - isatty() had the __fd_lock() call in the wrong place, which could have led to cleanup problems later. - The close action in the stdio, socket and termios hook code now also zaps the fd pointer itself after cleaning up the file descriptor table entry. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@15211 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/changes | 12 ++++++++++++ library/include/stdio.h | 10 +++++----- library/socket_hook_entry.c | 4 +++- library/stdio_fdhookentry.c | 3 ++- library/stdio_fwrite.c | 6 +++--- library/stdio_headers.h | 8 +++++--- library/termios_console_fdhookentry.c | 3 ++- library/unistd_isatty.c | 6 +++--- 8 files changed, 35 insertions(+), 17 deletions(-) diff --git a/library/changes b/library/changes index 39da771..fcf6f36 100644 --- a/library/changes +++ b/library/changes @@ -1,3 +1,15 @@ +- Reworked the __putc() and putc() macros to reference the 'c' input + parameter only once, and to be free of side-effects when tinkering + with the buffer position. + +- isatty() had the __fd_lock() call in the wrong place, which could have + led to cleanup problems later. + +- The close action in the stdio, socket and termios hook code now + also zaps the fd pointer itself after cleaning up the file descriptor + table entry. + + c.lib 1.205 (21.8.2010) - Added dlclose(), dlerror(), dlopen() and dlsym() functions, which are diff --git a/library/include/stdio.h b/library/include/stdio.h index 6bba79a..1e130ac 100644 --- a/library/include/stdio.h +++ b/library/include/stdio.h @@ -1,5 +1,5 @@ /* - * $Id: stdio.h,v 1.23 2010-10-19 09:35:16 obarthel Exp $ + * $Id: stdio.h,v 1.24 2010-10-20 13:12:59 obarthel Exp $ * * :ts=4 * @@ -285,11 +285,11 @@ extern char *tmpnam(char *buf); (((((FILE *)(f))->flags & (__FILE_IN_USE|__FILE_WRITABLE)) == (__FILE_IN_USE|__FILE_WRITABLE) && \ (((FILE *)(f))->flags & __FILE_BUFFER_MASK) != _IONBF && \ (((FILE *)(f))->num_write_bytes < ((FILE *)(f))->size)) ? \ - (((FILE *)(f))->buffer[((FILE *)(f))->num_write_bytes++] = (c), \ + (((FILE *)(f))->buffer[((FILE *)(f))->num_write_bytes] = (c), \ (((((FILE *)(f))->flags & __FILE_BUFFER_MASK) == _IOLBF && \ - (c) == '\n') ? \ - __flush(f) : \ - (c))) : \ + ((FILE *)(f))->buffer[((FILE *)(f))->num_write_bytes] == '\n') ? \ + ((FILE *)(f))->num_write_bytes++, __flush(f) : \ + ((FILE *)(f))->buffer[((FILE *)(f))->num_write_bytes++])) : \ fputc((c),(f))) /****************************************************************************/ diff --git a/library/socket_hook_entry.c b/library/socket_hook_entry.c index 51b75ff..a231042 100644 --- a/library/socket_hook_entry.c +++ b/library/socket_hook_entry.c @@ -1,5 +1,5 @@ /* - * $Id: socket_hook_entry.c,v 1.17 2006-11-16 10:41:15 obarthel Exp $ + * $Id: socket_hook_entry.c,v 1.18 2010-10-20 13:12:58 obarthel Exp $ * * :ts=4 * @@ -145,6 +145,8 @@ __socket_hook_entry( /* And that's the last for this file descriptor. */ memset(fd,0,sizeof(*fd)); + fd = NULL; + break; case file_action_seek: diff --git a/library/stdio_fdhookentry.c b/library/stdio_fdhookentry.c index e6c0789..3d2c5de 100644 --- a/library/stdio_fdhookentry.c +++ b/library/stdio_fdhookentry.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fdhookentry.c,v 1.34 2006-11-16 14:39:23 obarthel Exp $ + * $Id: stdio_fdhookentry.c,v 1.35 2010-10-20 13:12:58 obarthel Exp $ * * :ts=4 * @@ -367,6 +367,7 @@ __fd_hook_entry( /* And that's the last for this file descriptor. */ memset(fd,0,sizeof(*fd)); + fd = NULL; break; diff --git a/library/stdio_fwrite.c b/library/stdio_fwrite.c index 1e039de..3bc6e27 100644 --- a/library/stdio_fwrite.c +++ b/library/stdio_fwrite.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fwrite.c,v 1.11 2006-09-25 15:38:21 obarthel Exp $ + * $Id: stdio_fwrite.c,v 1.12 2010-10-20 13:12:58 obarthel Exp $ * * :ts=4 * @@ -134,7 +134,7 @@ fwrite(const void *ptr,size_t element_size,size_t count,FILE *stream) { c = (*data++); - if(__putc_line_buffered(c,(FILE *)file) < 0) + if(__putc_line_buffered(c,(FILE *)file) == EOF) goto out; total_bytes_written++; @@ -146,7 +146,7 @@ fwrite(const void *ptr,size_t element_size,size_t count,FILE *stream) { c = (*data++); - if(__putc_fully_buffered(c,(FILE *)file) < 0) + if(__putc_fully_buffered(c,(FILE *)file) == EOF) goto out; total_bytes_written++; diff --git a/library/stdio_headers.h b/library/stdio_headers.h index 27c1ed0..be278a5 100644 --- a/library/stdio_headers.h +++ b/library/stdio_headers.h @@ -1,5 +1,5 @@ /* - * $Id: stdio_headers.h,v 1.32 2010-10-19 09:35:16 obarthel Exp $ + * $Id: stdio_headers.h,v 1.33 2010-10-20 13:12:59 obarthel Exp $ * * :ts=4 * @@ -276,12 +276,13 @@ struct iob ((struct iob *)(f))->iob_Buffer[((struct iob *)(f))->iob_BufferPosition++] : \ __fgetc((FILE *)(f))) +/* Caution: this putc() variant will evaluate the 'c' parameter more than once. */ #define __putc(c,f,m) \ (((((struct iob *)(f))->iob_BufferWriteBytes < ((struct iob *)(f))->iob_BufferSize)) ? \ (((struct iob *)(f))->iob_Buffer[((struct iob *)(f))->iob_BufferWriteBytes++] = (c), \ (((m) == IOBF_BUFFER_MODE_LINE && (c) == '\n') ? \ __flush(f) : \ - (c))) : \ + ((c) & 255))) : \ __fputc((c),(f),(m))) #define __putc_fully_buffered(c,f) \ @@ -289,12 +290,13 @@ struct iob (((struct iob *)(f))->iob_Buffer[((struct iob *)(f))->iob_BufferWriteBytes++] = (c)) : \ __fputc((c),(f),IOBF_BUFFER_MODE_FULL)) +/* Caution: this putc() variant will evaluate the 'c' parameter more than once. */ #define __putc_line_buffered(c,f) \ (((((struct iob *)(f))->iob_BufferWriteBytes < ((struct iob *)(f))->iob_BufferSize)) ? \ (((struct iob *)(f))->iob_Buffer[((struct iob *)(f))->iob_BufferWriteBytes++] = (c), \ (((c) == '\n') ? \ __flush(f) : \ - ((c)))) : \ + ((c) & 255))) : \ __fputc((c),(f),IOBF_BUFFER_MODE_LINE)) /****************************************************************************/ diff --git a/library/termios_console_fdhookentry.c b/library/termios_console_fdhookentry.c index 360eea8..09db5c7 100755 --- a/library/termios_console_fdhookentry.c +++ b/library/termios_console_fdhookentry.c @@ -1,5 +1,5 @@ /* - * $Id: termios_console_fdhookentry.c,v 1.5 2006-11-16 14:39:23 obarthel Exp $ + * $Id: termios_console_fdhookentry.c,v 1.6 2010-10-20 13:12:59 obarthel Exp $ * * :ts=4 * @@ -474,6 +474,7 @@ __termios_console_hook( /* And that's the last for this file descriptor. */ memset(fd,0,sizeof(*fd)); + fd = NULL; break; diff --git a/library/unistd_isatty.c b/library/unistd_isatty.c index 8e1b227..491cfa4 100644 --- a/library/unistd_isatty.c +++ b/library/unistd_isatty.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_isatty.c,v 1.10 2008-04-16 07:46:05 obarthel Exp $ + * $Id: unistd_isatty.c,v 1.11 2010-10-20 13:12:59 obarthel Exp $ * * :ts=4 * @@ -67,14 +67,14 @@ isatty(int file_descriptor) goto out; } + __fd_lock(fd); + if(FLAG_IS_SET(fd->fd_Flags,FDF_IS_SOCKET)) { __set_errno(ENOTTY); goto out; } - __fd_lock(fd); - result = 1; if(FLAG_IS_CLEAR(fd->fd_Flags,FDF_IS_INTERACTIVE))