From 166d75e3c5ec53f1b6367f3b9fcc32e6510fbd2b Mon Sep 17 00:00:00 2001 From: chacha21 Date: Tue, 13 Dec 2016 10:40:06 +0100 Subject: [PATCH] Fix for unhandled error cases when using Media Foundation Some function calls to Media Foundation can fail and retrun null pointers. They should be checked before being dereferenced to avoid a crash. --- modules/videoio/src/cap_msmf.cpp | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/modules/videoio/src/cap_msmf.cpp b/modules/videoio/src/cap_msmf.cpp index c10a7c045e..a63e4fa59f 100644 --- a/modules/videoio/src/cap_msmf.cpp +++ b/modules/videoio/src/cap_msmf.cpp @@ -1986,13 +1986,17 @@ long videoDevice::resetDevice(IMFActivate *pActivate) &vd_pFriendlyName, NULL ); - hr = pActivate->ActivateObject( - __uuidof(IMFMediaSource), - (void**)&pSource - ); - enumerateCaptureFormats(pSource); - buildLibraryofTypes(); - SafeRelease(&pSource); + if (SUCCEEDED(hr)) + hr = pActivate->ActivateObject( + __uuidof(IMFMediaSource), + (void**)&pSource + ); + if (SUCCEEDED(hr) && pSource) + { + enumerateCaptureFormats(pSource); + buildLibraryofTypes(); + SafeRelease(&pSource); + }//end if (SUCCEEDED(hr) && pSource) if(FAILED(hr)) { vd_pFriendlyName = NULL; @@ -2638,7 +2642,12 @@ HRESULT videoDevice::enumerateCaptureFormats(IMFMediaSource *pSource) _ComPtr pSD = NULL; _ComPtr pHandler = NULL; _ComPtr pType = NULL; - HRESULT hr = pSource->CreatePresentationDescriptor(pPD.GetAddressOf()); + HRESULT hr = !pSource ? E_POINTER : S_OK; + if (FAILED(hr)) + { + goto done; + } + hr = pSource->CreatePresentationDescriptor(pPD.GetAddressOf()); if (FAILED(hr)) { goto done; @@ -3815,7 +3824,12 @@ HRESULT CvCaptureFile_MSMF::enumerateCaptureFormats(IMFMediaSource *pSource) _ComPtr pSD = NULL; _ComPtr pHandler = NULL; _ComPtr pType = NULL; - HRESULT hr = pSource->CreatePresentationDescriptor(pPD.GetAddressOf()); + HRESULT hr = !pSource ? E_POINTER : S_OK; + if (FAILED(hr)) + { + goto done; + } + hr = pSource->CreatePresentationDescriptor(pPD.GetAddressOf()); if (FAILED(hr)) { goto done;