diff options
| author | Siddhesh Poyarekar <siddhesh@sourceware.org> | 2024-11-07 11:16:04 -0500 |
|---|---|---|
| committer | Siddhesh Poyarekar <siddhesh@sourceware.org> | 2024-12-17 17:42:55 -0500 |
| commit | ae5062201d7e9d18fe88bff4bc71088374c394fb (patch) | |
| tree | 4f5917292a83f1e2f479802987f7e7dc966a5672 /libio/bits | |
| parent | cfdd9e7aa45cdc575df237e2d2eee3219a06829b (diff) | |
| download | glibc-ae5062201d7e9d18fe88bff4bc71088374c394fb.tar.xz glibc-ae5062201d7e9d18fe88bff4bc71088374c394fb.zip | |
ungetc: Guarantee single char pushback
The C standard requires that ungetc guarantees at least one pushback,
but the malloc call to allocate the pushback buffer could fail, thus
violating that requirement. Fix this by adding a single byte pushback
buffer in the FILE struct that the pushback can fall back to if malloc
fails.
The side-effect is that if the initial malloc fails and the 1-byte
fallback buffer is used, future resizing (if it succeeds) will be
2-bytes, 4-bytes and so on, which is suboptimal but it's after a malloc
failure, so maybe even desirable.
A future optimization here could be to have the pushback code use the
single byte buffer first and only fall back to malloc for subsequent
calls.
Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Reviewed-by: Maciej W. Rozycki <macro@redhat.com>
Diffstat (limited to 'libio/bits')
| -rw-r--r-- | libio/bits/types/struct_FILE.h | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/libio/bits/types/struct_FILE.h b/libio/bits/types/struct_FILE.h index d8d26639d1..87197a328c 100644 --- a/libio/bits/types/struct_FILE.h +++ b/libio/bits/types/struct_FILE.h @@ -1,4 +1,5 @@ /* Copyright (C) 1991-2024 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -70,7 +71,9 @@ struct _IO_FILE struct _IO_FILE *_chain; int _fileno; - int _flags2; + int _flags2:24; + /* Fallback buffer to use when malloc fails to allocate one. */ + char _short_backupbuf[1]; __off_t _old_offset; /* This used to be _offset but it's too small. */ /* 1+column number of pbase(); 0 is unknown. */ |
