From fe6b7b10ed98f19ab1a430db58b7ebe36c0175ce Mon Sep 17 00:00:00 2001 From: JohannesItten Date: Sun, 5 Jul 2026 12:46:00 +0300 Subject: [PATCH] fix: VideoReader crash on video-only file at EOF and dead bool return MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Guard swr_close/swr_init at EOF inside if (has_audio) — calling swr_init(nullptr) on a video-only file crashed at first loop - Change open_file from bool to void — it never returned false, only threw, so the if (!open_file()) check in the constructor was dead code Co-Authored-By: Claude Sonnet 4.6 --- shared/VideoReader.hpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/shared/VideoReader.hpp b/shared/VideoReader.hpp index 2ae087d..d7814b2 100644 --- a/shared/VideoReader.hpp +++ b/shared/VideoReader.hpp @@ -41,9 +41,7 @@ public: bool has_video = false; explicit VideoReader(const std::string& filename) { - if (!open_file(filename)) - return; - if (!has_video && !has_audio) return; + open_file(filename); get_source_info(); if (has_video) allocate_video_conversion_buffers(); if (has_audio) allocate_audio_conversion_buffers(); @@ -134,9 +132,11 @@ public: // EOF — loop back to start avformat_seek_file(format_context, -1, 0, 0, 0, AVSEEK_FLAG_BACKWARD); if (has_video) avcodec_flush_buffers(video_codec_context); - if (has_audio) avcodec_flush_buffers(audio_codec_context); - swr_close(swr_audio_ctx); - swr_init(swr_audio_ctx); + if (has_audio) { + avcodec_flush_buffers(audio_codec_context); + swr_close(swr_audio_ctx); + swr_init(swr_audio_ctx); + } continue; } @@ -166,7 +166,7 @@ private: AVFrame* audio_frame = av_frame_alloc(); SwrContext* swr_audio_ctx = nullptr; - bool open_file(const std::string& filename) { + void open_file(const std::string& filename) { if (avformat_open_input(&format_context, filename.c_str(), nullptr, nullptr) != 0) throw std::runtime_error("Could not open file: " + filename); @@ -186,12 +186,10 @@ private: } } - if (video_stream_index == -1 && audio_stream_index == -1) { + if (!has_video && !has_audio) { avformat_close_input(&format_context); throw std::runtime_error("No audio/video stream found in: " + filename); } - - return true; } void get_source_info() {