From b79e7d2eee7ccb336d1275cab89730e0eb0b60ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Mon, 25 Jul 2016 20:24:30 +0000 Subject: [PATCH] =?UTF-8?q?[MMSYS]:=20Improve=20AddSoundProfile,=20by=20Vi?= =?UTF-8?q?ctor=20Martinez=20Calvo=20(with=201=20minor=20modification=20by?= =?UTF-8?q?=20myself):=20=E2=97=BE=20Don't=20return=20TRUE=20when=20CB=5FS?= =?UTF-8?q?ETITEMDATA=20fails.=20=E2=97=BE=20Avoid=20pScheme=20NULL=20dere?= =?UTF-8?q?ference=20if=20allocation=20fails.=20CID=201223154=20=E2=97=BE?= =?UTF-8?q?=20Avoid=20overflowing=20the=20destiny=20buffer=20by=20using=20?= =?UTF-8?q?StringCchCopy()=20CID=20510953=20and:=201)=20Make=20it=20more?= =?UTF-8?q?=20readable=20by=20returning=20as=20soon=20as=20possible=20when?= =?UTF-8?q?=20an=20error=20happens.=202)=20Make=20it=20more=20readable=20b?= =?UTF-8?q?y=20reducing=20the=20nested=20ifs-checks.=203)=20Remove=20the?= =?UTF-8?q?=20added=20string=20in=20the=20combobox=20if=20the=20sound=20sc?= =?UTF-8?q?heme=20buffer=20mem=20allocation=20fails=20(pt.3=20modified).?= =?UTF-8?q?=20CORE-11603=20#resolve=20#comment=20Thanks!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit svn path=/trunk/; revision=72000 --- reactos/dll/cpl/mmsys/sounds.c | 51 +++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/reactos/dll/cpl/mmsys/sounds.c b/reactos/dll/cpl/mmsys/sounds.c index 8d5c5357191..1ad48632ee5 100644 --- a/reactos/dll/cpl/mmsys/sounds.c +++ b/reactos/dll/cpl/mmsys/sounds.c @@ -5,11 +5,14 @@ * PROGRAMMER: Thomas Weidenmueller * Johannes Anderwald * Dmitry Chapyshev + * Victor Martinez Calvo */ #include "mmsys.h" #include +#include + #include struct __APP_MAP__; @@ -291,6 +294,8 @@ AddSoundProfile(HWND hwndDlg, HKEY hKey, TCHAR * szSubKey, BOOL SetDefault) HKEY hSubKey; TCHAR szValue[MAX_PATH]; DWORD dwValue, dwResult; + LRESULT lResult; + PSOUND_SCHEME_CONTEXT pScheme; if (RegOpenKeyEx(hKey, szSubKey, @@ -309,27 +314,35 @@ AddSoundProfile(HWND hwndDlg, HKEY hKey, TCHAR * szSubKey, BOOL SetDefault) (LPBYTE)szValue, &dwValue); RegCloseKey(hSubKey); - if (dwResult == ERROR_SUCCESS) - { - LRESULT lResult = SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_ADDSTRING, (WPARAM)0, (LPARAM)szValue); - if (lResult != CB_ERR) - { - PSOUND_SCHEME_CONTEXT pScheme = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SOUND_SCHEME_CONTEXT)); - if (pScheme != NULL) - { - _tcscpy(pScheme->szDesc, szValue); - _tcscpy(pScheme->szName, szSubKey); - } - SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_SETITEMDATA, (WPARAM)lResult, (LPARAM)pScheme); - if (SetDefault) - { - SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_SETCURSEL, (WPARAM)lResult, (LPARAM)0); - } - } - return TRUE; + if (dwResult != ERROR_SUCCESS) + return FALSE; + + /* Try to add the new profile */ + lResult = SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_ADDSTRING, (WPARAM)0, (LPARAM)szValue); + if (lResult == CB_ERR) + return FALSE; + + /* Allocate the profile scheme buffer */ + pScheme = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SOUND_SCHEME_CONTEXT)); + if (pScheme == NULL) + { + /* We failed to allocate the buffer, no need to keep a dangling string in the combobox */ + SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_DELETESTRING, (WPARAM)lResult, (LPARAM)0); + return FALSE; } - return FALSE; + + StringCchCopy(pScheme->szDesc, MAX_PATH, szValue); + StringCchCopy(pScheme->szName, MAX_PATH, szSubKey); + + /* Associate the value with the item in the combobox */ + SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_SETITEMDATA, (WPARAM)lResult, (LPARAM)pScheme); + + /* Optionally, select the profile */ + if (SetDefault) + SendDlgItemMessage(hwndDlg, IDC_SOUND_SCHEME, CB_SETCURSEL, (WPARAM)lResult, (LPARAM)0); + + return TRUE; }