From 54bc6a61f5e64fa593138f91a681edf46afd68e3 Mon Sep 17 00:00:00 2001 From: "henrika@google.com" Date: Mon, 20 Jun 2011 09:41:22 +0000 Subject: [PATCH] Improves quality of AudioDeviceWindowsCore::_GetDeviceName. The current version can crash if the output string is invalid. Review URL: http://webrtc-codereview.appspot.com/45002 git-svn-id: http://webrtc.googlecode.com/svn/trunk@100 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../Windows/audio_device_windows_core.cc | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/modules/audio_device/main/source/Windows/audio_device_windows_core.cc b/modules/audio_device/main/source/Windows/audio_device_windows_core.cc index f965e6b19f..3b1ae15919 100644 --- a/modules/audio_device/main/source/Windows/audio_device_windows_core.cc +++ b/modules/audio_device/main/source/Windows/audio_device_windows_core.cc @@ -4023,7 +4023,9 @@ WebRtc_Word32 AudioDeviceWindowsCore::_GetDefaultDeviceID(EDataFlow dir, ERole r // _GetDeviceName // ---------------------------------------------------------------------------- -WebRtc_Word32 AudioDeviceWindowsCore::_GetDeviceName(IMMDevice* pDevice, LPWSTR pszBuffer, int bufferLen) +WebRtc_Word32 AudioDeviceWindowsCore::_GetDeviceName(IMMDevice* pDevice, + LPWSTR pszBuffer, + int bufferLen) { WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "%s", __FUNCTION__); @@ -4033,25 +4035,51 @@ WebRtc_Word32 AudioDeviceWindowsCore::_GetDeviceName(IMMDevice* pDevice, LPWSTR IPropertyStore *pProps = NULL; PROPVARIANT varName; - // Initialize container for property value. - PropVariantInit(&varName); - assert(pszBuffer != NULL); assert(bufferLen > 0); if (pDevice != NULL) { hr = pDevice->OpenPropertyStore(STGM_READ, &pProps); - if (hr == S_OK) + if (FAILED(hr)) { - // Get the endpoint device's friendly-name property. - hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName); + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, + "IMMDevice::OpenPropertyStore failed, hr = 0x%08X", hr); } } - if (hr == S_OK) + // Initialize container for property value. + PropVariantInit(&varName); + + if (SUCCEEDED(hr)) { - // Found the device name. + // Get the endpoint device's friendly-name property. + hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName); + if (FAILED(hr)) + { + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, + "IPropertyStore::GetValue failed, hr = 0x%08X", hr); + } + } + + if ((SUCCEEDED(hr)) && (VT_EMPTY == varName.vt)) + { + hr = E_FAIL; + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, + "IPropertyStore::GetValue returned no value, hr = 0x%08X", hr); + } + + if ((SUCCEEDED(hr)) && (VT_LPWSTR != varName.vt)) + { + // The returned value is not a wide null terminated string. + hr = E_UNEXPECTED; + WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, + "IPropertyStore::GetValue returned unexpected type, hr = 0x%08X", hr); + } + + if (SUCCEEDED(hr) && (varName.pwszVal != NULL)) + { + // Copy the valid device name to the provided ouput buffer. wcsncpy_s(pszBuffer, bufferLen, varName.pwszVal, _TRUNCATE); } else