bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new module 'fpurge'


From: Bruno Haible
Subject: Re: new module 'fpurge'
Date: Mon, 23 Apr 2007 10:46:34 +0200
User-agent: KMail/1.5.4

Eric Blake wrote:
> > Order of operations.  I'm glad to know I'm not the only one that makes this 
> > mistake.
> 
> Actually, I found the real problem, with a bit more looking at the code.  
> With 
> this patch, test-fpurge passes on cygwin

Thanks for these fixes. Sorry, I was in a hurry, and don't have a Cygwin
system for testing.

> It also lacks the HASUB/FREEUB macro usage present in the MacOS version of 
> fpurge, which means you may have introduced a memory leak.

Indeed. It is quite rare, since pushing back more than 1 byte or pushing back
a byte that was not previously read from the stream is not covered by ISO C 99
(as far as I understand; in particular Solaris and mingw don't support it), but
you are right that if the stream is in that state, fpurge() should do something
sensible.


2007-04-23  Bruno Haible  <address@hidden>

        * lib/fpurge.c (fpurge) [glibc, BSD]: Free a malloc()ed ungetc buffer.
        Reported by Eric Blake.

*** lib/fpurge.c        16 Apr 2007 15:02:36 -0000      1.2
--- lib/fpurge.c        23 Apr 2007 08:42:51 -0000
***************
*** 29,34 ****
--- 29,40 ----
  #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
    fp->_IO_read_end = fp->_IO_read_ptr;
    fp->_IO_write_ptr = fp->_IO_write_base;
+   /* Avoid memory leak when there is an active ungetc buffer.  */
+   if (fp->_IO_save_base != NULL)
+     {
+       free (fp->_IO_save_base);
+       fp->_IO_save_base = NULL;
+     }
    return 0;
  #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, 
Cygwin */
    fp->_p = fp->_bf._base;
***************
*** 36,41 ****
--- 42,61 ----
    fp->_w = ((fp->_flags & (__SLBF | __SNBF)) == 0 /* fully buffered? */
            ? fp->_bf._size
            : 0);
+   /* Avoid memory leak when there is an active ungetc buffer.  */
+ # if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
+    /* See 
<http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
+       and 
<http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
 */
+ #  define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
+ # else                                         /* FreeBSD, MacOS X, Cygwin */
+ #  define fp_ub fp->_ub
+ # endif
+   if (fp_ub._base != NULL)
+     {
+       if (fp_ub._base != fp->_ubuf)
+       free (fp_ub._base);
+       fp_ub._base = NULL;
+     }
    return 0;
  #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, 
mingw */
    fp->_ptr = fp->_base;
***************
*** 43,48 ****
      fp->_cnt = 0;
    return 0;
  #else
!  #error "Please port gnulib fpurge.c to your platform!"
  #endif
  }
--- 63,68 ----
      fp->_cnt = 0;
    return 0;
  #else
!  #error "Please port gnulib fpurge.c to your platform! Look at the 
definitions of fflush, setvbuf and ungetc on your system, then report this to 
bug-gnulib."
  #endif
  }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]