[PATCH v3] ungetc: Guarantee single char pushback

Maciej W. Rozycki macro@redhat.com
Mon Dec 9 01:40:27 GMT 2024


On Fri, 6 Dec 2024, Siddhesh Poyarekar wrote:

> The C standard requires that ungetc guarantees at least one pushback, so
> put a single byte pushback buffer in the FILE struct to enable that.

 So what would the problem be if we instead replaced:

  int _flags2;

with

  short int _flags2;
  char _short_backupbuf[1];
  char _unused;

or if we wanted to be super cautious and used a union to prevent any issue 
with alignment with some obscure psABI (which I doubt is needed given that 
the preceding member is of the int type), then:

  union
    {
      int _overlay;
      struct
	{
	  short int _flags2;
	  char _short_backupbuf[1];
	  char _unused;
	};
    };

?  There's no need to change existing code that uses _flags2 and there's 
no need to penalise legacy calls.  I guess saving _unused2 space is hardly 
an argument, though I note that placing a char[1] member first will ask 
for rearrangement to avoid wasting space for padding once a wider member 
follows in the future.

 I've run native `powerpc64le-linux-gnu' regression testing and saw no 
issues with either patch attached applied on top of your change.

  Maciej
-------------- next part --------------
A non-text attachment was scrubbed...
Name: glibc-struct-file-flags2-short-backupbuf.diff
Type: text/x-diff
Size: 2671 bytes
Desc: 
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20241209/400b48dd/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: glibc-struct-file-flags2-short-backupbuf-union.diff
Type: text/x-diff
Size: 2902 bytes
Desc: 
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20241209/400b48dd/attachment-0003.bin>


More information about the Libc-alpha mailing list