From 287a6e8473cb40396b00556ced4f9b275f54ff8e Mon Sep 17 00:00:00 2001 From: Andrew Eikum Date: Fri, 2 Dec 2011 11:53:14 -0600 Subject: [PATCH] dsound: Validate format in primary buffer's SetFormat(). --- dlls/dsound/primary.c | 6 ++ dlls/dsound/tests/dsound.c | 139 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/dlls/dsound/primary.c b/dlls/dsound/primary.c index 3b61562ccac..1cc59e55779 100644 --- a/dlls/dsound/primary.c +++ b/dlls/dsound/primary.c @@ -374,6 +374,12 @@ HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passe passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign, passed_fmt->wBitsPerSample, passed_fmt->cbSize); + if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 || + passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 || + passed_fmt->nAvgBytesPerSec == 0 || + passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8) + return DSERR_INVALIDPARAM; + /* **** */ RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE); EnterCriticalSection(&(device->mixlock)); diff --git a/dlls/dsound/tests/dsound.c b/dlls/dsound/tests/dsound.c index 0c0239c4d27..45dd00a6fd6 100644 --- a/dlls/dsound/tests/dsound.c +++ b/dlls/dsound/tests/dsound.c @@ -1221,6 +1221,144 @@ EXIT: return rc; } +static HRESULT test_invalid_fmts(LPGUID lpGuid) +{ + HRESULT rc; + LPDIRECTSOUND dso=NULL; + LPDIRECTSOUNDBUFFER primary=NULL; + DSBUFFERDESC bufdesc; + + /* Create the DirectSound object */ + rc=pDirectSoundCreate(lpGuid,&dso,NULL); + ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED, + "DirectSoundCreate() failed: %08x\n",rc); + if (rc!=DS_OK) + return rc; + + /* We must call SetCooperativeLevel before creating primary buffer */ + /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */ + rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY); + ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc); + if (rc!=DS_OK){ + IDirectSound_Release(dso); + return rc; + } + + ZeroMemory(&bufdesc, sizeof(bufdesc)); + bufdesc.dwSize=sizeof(bufdesc); + bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER; + rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL); + ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed " + "to create a primary buffer %08x\n",rc); + + if (rc==DS_OK && primary!=NULL) { + WAVEFORMATEX wfx; + + wfx.wFormatTag = WAVE_FORMAT_PCM; + wfx.nChannels = 0; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 0; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 2; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 12; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 0; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = 0; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = 0; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample / 8) - 1; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample / 8) + 1; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign + 1; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == S_OK, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign - 1; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == S_OK, "SetFormat: %08x\n", rc); + + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign + 1; + rc = IDirectSoundBuffer_SetFormat(primary, &wfx); + ok(rc == S_OK, "SetFormat: %08x\n", rc); + + IDirectSoundBuffer_Release(primary); + } + + IDirectSound_Release(dso); + + return S_OK; +} + static unsigned int number; static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription, @@ -1250,6 +1388,7 @@ static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription, test_secondary(lpGuid); test_frequency(lpGuid); test_duplicate(lpGuid); + test_invalid_fmts(lpGuid); } return 1; -- 2.11.4.GIT