|
|
Message-ID: <20260805190349.GT27423@brightrain.aerifal.cx>
Date: Wed, 5 Aug 2026 15:03:49 -0400
From: Rich Felker <dalias@...c.org>
To: Matthias Goergens <matthias.goergens@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH 2/2] stdio: report overflow in buffered stream
position
On Wed, Aug 05, 2026 at 03:52:01PM +0800, Matthias Goergens wrote:
> ftello adds pending buffered output to the position reported by the
> underlying seek operation. Near LLONG_MAX, the addition can overflow
> signed off_t and return an apparently successful negative position.
>
> Check that the buffered-byte count fits before adding it. Fail with
> EOVERFLOW when the logical position cannot be represented.
> ---
> src/stdio/ftell.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/src/stdio/ftell.c b/src/stdio/ftell.c
> index 1e1a08d8..c2f104b1 100644
> --- a/src/stdio/ftell.c
> +++ b/src/stdio/ftell.c
> @@ -12,8 +12,13 @@ off_t __ftello_unlocked(FILE *f)
> /* Adjust for data in buffer. */
> if (f->rend)
> pos += f->rpos - f->rend;
> - else if (f->wbase)
> + else if (f->wbase) {
> + if (f->wpos - f->wbase > LLONG_MAX - pos) {
> + errno = EOVERFLOW;
> + return -1;
> + }
> pos += f->wpos - f->wbase;
> + }
> return pos;
> }
This one is probably the only correct way to handle the condition, but
nasty. It's really not nice that ftello can fail on a seekable file on
which no error has been seen. Fortunately, I don't think it matters
either way, at least not for most environments where exabyte-scale
files are just not a thing.
Rich
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.