From f7a467f88239f703df40103f75e8d30fc8706368 Mon Sep 17 00:00:00 2001 From: Teddy-Yangjiale <12411723@mail.sustech.edu.cn> Date: Tue, 5 May 2026 20:55:52 +0800 Subject: [PATCH] Merge pull request #28914 from Teddy-Yangjiale:fix-issue-28904 videoio(dshow): fix crash in videoInput::start() when pVih is NULL #28914 ### Summary This PR fixes a critical application crash in the DirectShow backend when encountering certain legacy or virtual cameras (e.g., Microsoft's DirectShow "Ball" Sample). ### The Problem In `modules/videoio/src/cap_dshow.cpp`, the function `videoInput::start()` calls `pConfig->GetConnectedMediaType(&mt)`. - In some edge cases, this call returns `S_OK`, indicating success. - However, the associated format block `mt.pbFormat` (which `pVih` points to) can still be `NULL`. - The current implementation uses `CV_Assert(pVih)`, which triggers a hard crash of the entire application when this occurs. ### The Fix I have replaced the `CV_Assert(pVih)` with a null-pointer check. If `pVih` is `NULL`, the function now returns `false`. This allows the high-level `cv::VideoCapture` to handle the failure (e.g., by returning `false` from `.open()`) instead of crashing the process. ### Engineering Rationale - Execution Path Consistency: For devices providing valid format headers, `pVih` remains non-NULL. In these cases, the conditional check is skipped, and the execution path is identical to the original implementation. - Process Stability: Replacing `CV_Assert` prevents immediate process termination (SIGABRT) when a DirectShow filter provides an invalid or empty format block. - Error Propagation: Returning `false` in `videoInput::start()` allows the initialization failure to propagate through the call stack. This ensures that `cv::VideoCapture::open()` returns `false`, enabling the caller to detect the issue via the standard API (e.g., `isOpened()`) instead of the process being terminated. ### Evidence I have manually reproduced this crash using the Microsoft DirectShow Ball Sample filter on Windows. As shown in the attached debugger screenshot: - `deviceID` is 2 (the virtual camera). - `hr` is `S_OK`. - `pVih` is `NULL`. dshow_null_pvih_debug_evidence Fixes #28904 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/videoio/src/cap_dshow.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/videoio/src/cap_dshow.cpp b/modules/videoio/src/cap_dshow.cpp index 15eddfbe1e..f809802831 100644 --- a/modules/videoio/src/cap_dshow.cpp +++ b/modules/videoio/src/cap_dshow.cpp @@ -2756,7 +2756,13 @@ int videoInput::start(int deviceID, videoDevice *VD){ } VIDEOINFOHEADER *pVih = reinterpret_cast(VD->pAmMediaType->pbFormat); - CV_Assert(pVih); + // Some legacy or virtual cameras (e.g., Microsoft Ball filter) return S_OK + // but leave pbFormat as NULL. We check for NULL here to avoid a crash. + // https://github.com/opencv/opencv/issues/28904 + if (pVih == NULL) { + DebugPrintOut("ERROR: pbFormat field is not set!\n"); + return false; + } int currentWidth = HEADER(pVih)->biWidth; int currentHeight = HEADER(pVih)->biHeight;