From dcd17a6a7cbe24d02356516dd052c9af40ef9dda Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Mon, 17 Aug 2026 00:06:20 -0700 Subject: [PATCH] fix(file_functions): check the seek that can fail instead of one that cannot buffered_read_opt()'s seek branch guarded against moving before the start of the file with "op + bytes < 0". That could not fire: bytes is unsigned, so the sum was evaluated unsigned and never went negative, and every caller passes a forward distance anyway. What can fail is LSEEK itself. On error it returns -1, and the code then computed "np - op" from two error values and reported the result as a byte count. Today that lands on 0 and the loop exits, so the observable behaviour is unchanged -- but only by arithmetic accident, and the next reader has no way to tell that the guard above it was inert. Check what actually fails, and return the bytes copied so far rather than inventing a distance from two failures. Noted while reviewing the MSVC narrowing warnings for #2325, which flagged this line as dead rather than wrong and left it out of that change deliberately. --- src/lib_ccx/file_functions.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib_ccx/file_functions.c b/src/lib_ccx/file_functions.c index 8a46bb6f1..a63fd8845 100644 --- a/src/lib_ccx/file_functions.c +++ b/src/lib_ccx/file_functions.c @@ -371,9 +371,18 @@ size_t buffered_read_opt(struct ccx_demuxer *ctx, unsigned char *buffer, size_t { LLONG op, np; op = LSEEK(ctx->infd, 0, SEEK_CUR); // Get current pos - if (op + bytes < 0) // Would mean moving beyond start of file: Not supported - return 0; + /* The guard here used to be "op + bytes < 0", meant to catch a + seek before the start of the file. It could not fire: bytes is + unsigned, so the sum was computed unsigned and never went + negative, and every caller passes a forward distance anyway. + What can actually fail is LSEEK itself, and the arithmetic + below then subtracted one error value from another and + reported the result as a byte count. */ + if (op < 0) + return copied; np = LSEEK(ctx->infd, bytes, SEEK_CUR); // Pos after moving + if (np < 0) + return copied; i = (int)(np - op); } // if both above lseek returned -1 (error); i would be 0 here and