PR #26241 opened by Jun Zhao (mypopydev)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26241
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26241.patch

A reply with Transfer-Encoding: chunked and no Content-Length leaves
filesize == UINT64_MAX, so the length comparisons added by 62006041cc
("don't return EOF unless we actually read all bytes") are always true.
Once the terminating chunk closes the connection, the next read hits
`!s->hd` and returns AVERROR(EIO) instead of EOF, even though the whole
body was delivered. dashdec.c's parse_manifest() then fails with
"Unable to read to manifest"; -multiple_requests 1 masks it.

Guard the three length comparisons on != UINT64_MAX so a clean close of
an unknown-length resource is EOF while a declared-length truncation
still returns EIO and a transport error stays premature (so reconnect
can recover it).

This adapts only the != UINT64_MAX guards from master's 6865eb18f8 to
the 9.0 code, which compares s->filesize/target_end directly and has no
requested_end(). The chunked-framing strictness, reconnect-loop and
HEAD changes from that commit are not part of this backport.

Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/26199

Signed-off-by: Jun Zhao <[email protected]>

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From 4d152d65f6b8ef1d2d6a19e22da83d0fa518e66c Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Sat, 21 Nov 2026 08:17:42 +0800
Subject: [PATCH] avformat/http: treat unknown-length clean EOF as EOF, not EIO

A reply with Transfer-Encoding: chunked and no Content-Length leaves
filesize == UINT64_MAX, so the length comparisons added by 62006041cc
("don't return EOF unless we actually read all bytes") are always true.
Once the terminating chunk closes the connection, the next read hits
`!s->hd` and returns AVERROR(EIO) instead of EOF, even though the whole
body was delivered. dashdec.c's parse_manifest() then fails with
"Unable to read to manifest"; -multiple_requests 1 masks it.

Guard the three length comparisons on != UINT64_MAX so a clean close of
an unknown-length resource is EOF while a declared-length truncation
still returns EIO and a transport error stays premature (so reconnect
can recover it).

This adapts only the != UINT64_MAX guards from master's 6865eb18f8 to
the 9.0 code, which compares s->filesize/target_end directly and has no
requested_end(). The chunked-framing strictness, reconnect-loop and
HEAD changes from that commit are not part of this backport.

Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/26199

Signed-off-by: Jun Zhao <[email protected]>
---
 libavformat/http.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 35f886fba7f..3bc34bb0a29 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -1776,7 +1776,8 @@ static int http_buf_read(URLContext *h, uint8_t *buf, int 
size)
             return AVERROR(EAGAIN); /* reached end of content range */
         len = ffurl_read(s->hd, buf, size);
         if ((!len || len == AVERROR_EOF) &&
-            (!s->willclose || s->chunksize == UINT64_MAX) && s->off < 
target_end) {
+            (!s->willclose || s->chunksize == UINT64_MAX) &&
+            target_end != UINT64_MAX && s->off < target_end) {
             av_log(h, AV_LOG_ERROR,
                    "Stream ends prematurely at %"PRIu64", should be 
%"PRIu64"\n",
                    s->off, target_end
@@ -1839,7 +1840,8 @@ static int http_read_stream(URLContext *h, uint8_t *buf, 
int size)
     int conn_attempts = 1;
 
     if (!s->hd)
-        return s->off < s->filesize ? AVERROR(EIO) : AVERROR_EOF;
+        return s->filesize != UINT64_MAX && s->off < s->filesize
+               ? AVERROR(EIO) : AVERROR_EOF;
 
     if (s->end_chunked_post && !s->end_header) {
         err = http_read_header(h);
@@ -1856,7 +1858,11 @@ retry:
     read_ret = http_buf_read(h, buf, size);
     while (read_ret < 0) {
         uint64_t target = h->is_streamed ? 0 : s->off;
-        bool is_premature = s->filesize > 0 && s->off < s->filesize;
+        /* Unknown length: a clean close is EOF. A transport error still
+         * means bytes are missing, so reconnect can recover it. */
+        bool is_premature = s->filesize > 0 && s->off < s->filesize &&
+                            (s->filesize != UINT64_MAX ||
+                             read_ret != AVERROR_EOF);
 
         if (read_ret == AVERROR_EXIT)
             break;
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to