This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem with line buffering and getc function on 1.7.33.

On Mar 12 16:37, Kaz Kylheku wrote:
> On 12.03.2016 14:29, Corinna Vinschen wrote:
> >On Mar 12 20:39, Corinna Vinschen wrote:
> >>On Mar 11 16:05, Kaz Kylheku wrote:
> >>> We can reproduce the problem with just file streams using
> >>> a much simpler program:
> >>> [...]
> >>> The contents of file end up being "\n": one empty
> >>> line, instead of "abcde\n":
> >>>
> >>>   $ cat file
> >>>
> >>>   $
> >>
> >>Thanks for the testcase.  I can reproduce the issue and I see where the
> >>problem occurs, but I'm still puzzled.  Comparing the code in our newlib
> >>C library with its BSD counterparts, I could swear the same behaviour
> >>happens on OpenBSD as well.  If not (which needs testing), I wonder why
> >>and where newlib's actually different.  Right now I don't see the
> >>difference.
> >
> >I do now.  Basically it's setvbuf screwing up the internal flags in the
> >FILE structure.  I took the liberty to update newlib's setvbuf to the
> >OpenBSD version locally and I'm going to apply my patches to newlib
> >soon.  I'll provide a new 2.5.0 test release of Cygwin with this patch
> >tomorrow or early next week.
> Indeed, I thought it would have something to do with the
> stream->_flags, because when I single-stepped over the getc(stream)
> line (necessary for the problem to repro, IIRC) the only apparent state
> change in the stream was to the _flags member:

The actual problem was that setvbuf set the _lbfsize member to -bufsize
in the _IOLBF case indiscriminately.  This in turn leads to __sputc_r
doing the wrong thing:

  if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
    return (*_p->_p++ = _c);
    return (__swbuf_r(_ptr, _c, _p));

Note the check for _p->_w >= _p->_lbfsize.  By setting _lbfsize in
setvbuf, what happens is that the first branch is called for all
chars up to the first \n.  However, the *first* call to __sputc_r
is supposed to call __swbuf_r anyway, otherwise bookkeeping is broken.

And that's the major difference to the OpenBSD setvbuf; it only sets
fp->_lbfsize to -bufsize if we're already writing (__SWR flag is set).


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

Attachment: signature.asc
Description: PGP signature

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]