[PATCH v2] newlib: libc: Fix bugs in the commit 3d94e07c49b5.

Corinna Vinschen vinschen@redhat.com
Thu Nov 16 14:08:07 GMT 2023


On Nov 16 14:48, Corinna Vinschen wrote:
> On Nov 16 14:19, Corinna Vinschen wrote:
> > On Nov 16 21:27, Takashi Yano wrote:
> > > 2) __swbuf_r() has ORIENT (fp, -1) call, however, result is not checked.
> > 
> > Good catch.  Looks like setting the orientation at this point is
> > wrong.  __swbuf_r is called via the __sputc_r macro in stdio.h.
> > But __sputc_r is also called from __fputwc as well as, potentially,
> > from _fputws_r.
> > 
> > AFAICS, we already set the orientation from the calling functions
> > _putc_r (covering putchar, putc, fputc) and from _puts_r as
> > well as _fputs_r (covering puts, fputs).
> > 
> > On the wide-char side, we set the orientation from _fputwc_r
> > (covering putwchar, putwc, fputwc) and from _fputws_r (covering
> > fputws).
> > 
> > So I *think* we can safely remove the ORIENT call from __swbuf_r,
> > or did I miss some other way to call __swbuf_r?
> 
> Yes, I did.  The _getchar_unlocked and _putchar_unlocked macros as well
> as the __SINGLE_THREAD__ getc and putc macros(*).  I also missed the
> putc{har}_unlocked and getc{har}_unlocked functions.
> 
> They all call __sgetc_r or __sputs_r, both being macros in stdio.h.
> The underlying functions __srget_r and __swbuf_r are shared between
> byte and wide-char orientation functions.
> 
> Drat.  Looks like we really have to do the ORIENT stuff inside the
> __sgetc_r/__sputs_r macros...

That's not feasible.  The only way out as I see it is that we don't
share the underlying functions and macros between byte and wide-char
functions:

- _fputwc_r and _fputws_r must not call __sputc_r from stdio.h, they
  should get their own version.

- The underlying functions __srget_r and __swbuf_r have to be split into
  two functions each, or they have to get an additional parameter
  deciding how to set the orientation.

- In turn, we can remove most of the ORIENT calls in the calling functions.


Corinna



More information about the Newlib mailing list