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/oldfileops.c | |
| 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/oldfileops.c')
| -rw-r--r-- | libio/oldfileops.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/libio/oldfileops.c b/libio/oldfileops.c index 8f775c9094..03f4d76a57 100644 --- a/libio/oldfileops.c +++ b/libio/oldfileops.c @@ -1,4 +1,5 @@ /* Copyright (C) 1993-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 @@ -311,7 +312,7 @@ _IO_old_file_underflow (FILE *fp) /* Maybe we already have a push back pointer. */ if (fp->_IO_save_base != NULL) { - free (fp->_IO_save_base); + _IO_free_backup_buf (fp, fp->_IO_save_base); fp->_flags &= ~_IO_IN_BACKUP; } _IO_doallocbuf (fp); @@ -464,7 +465,7 @@ _IO_old_file_seekoff (FILE *fp, off64_t offset, int dir, int mode) /* It could be that we already have a pushback buffer. */ if (fp->_IO_read_base != NULL) { - free (fp->_IO_read_base); + _IO_free_backup_buf (fp, fp->_IO_read_base); fp->_flags &= ~_IO_IN_BACKUP; } _IO_doallocbuf (fp); |
