From 161b7981f9bc8014583caa2eb14ac3a488fba8ce Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 29 Mar 2026 21:46:10 -0700 Subject: [PATCH] fix: video player decode loop could spin indefinitely on corrupt files The while(true) loop retried av_read_frame after seeking to the start on error. A corrupt file where read fails but seek succeeds would loop forever, blocking the main thread. Bounded to 500 attempts with a warning log on exhaustion. --- src/rendering/video_player.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rendering/video_player.cpp b/src/rendering/video_player.cpp index c11dd799..dc9d94fd 100644 --- a/src/rendering/video_player.cpp +++ b/src/rendering/video_player.cpp @@ -187,7 +187,11 @@ bool VideoPlayer::decodeNextFrame() { AVPacket* pkt = reinterpret_cast(packet); SwsContext* sws = reinterpret_cast(swsCtx); - while (true) { + // Cap iterations to prevent infinite spinning on corrupt/truncated video + // files where av_read_frame fails but av_seek_frame succeeds, looping + // endlessly through the same corrupt region. + constexpr int kMaxDecodeAttempts = 500; + for (int attempt = 0; attempt < kMaxDecodeAttempts; ++attempt) { int ret = av_read_frame(fmt, pkt); if (ret < 0) { if (av_seek_frame(fmt, videoStreamIndex, 0, AVSEEK_FLAG_BACKWARD) >= 0) { @@ -224,6 +228,8 @@ bool VideoPlayer::decodeNextFrame() { uploadFrame(); return true; } + LOG_WARNING("Video decode: exceeded ", kMaxDecodeAttempts, " attempts — possible corrupt file"); + return false; } void VideoPlayer::uploadFrame() {