Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260805075201.3508375-2-matthias.goergens@gmail.com>
Date: Wed,  5 Aug 2026 15:52:01 +0800
From: Matthias Goergens <matthias.goergens@...il.com>
To: musl@...ts.openwall.com
Cc: Matthias Goergens <matthias.goergens@...il.com>
Subject: [PATCH 2/2] stdio: report overflow in buffered stream position

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;
 }
 
-- 
2.55.0

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.