From 6bb0234dc899c69c819eeda4ac69965f6db69d21 Mon Sep 17 00:00:00 2001 From: jackchentwkh Date: Sun, 23 May 2021 15:58:39 +0800 Subject: [PATCH] Fix DSoundBufferResizeSetSize with DSBuffer creation failure when remained hos buffer bytes is less then DSBSIZE_MIN, also correct a wrongly used argument with call to DSound3DBufferCreate. add more sanity checks. this fixs PGR2, now release build can enter the race. but debug build has different issue with vector/iterator. --- .../DSOUND/DirectSound/DirectSoundInline.hpp | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp b/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp index 66c6a2714..a965800f8 100644 --- a/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp +++ b/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp @@ -371,12 +371,19 @@ static inline void DSoundGenericUnlock( static inline HRESULT DSoundBufferCreate(LPDSBUFFERDESC pDSBufferDesc, LPDIRECTSOUNDBUFFER8 &pDSBuffer) { LPDIRECTSOUNDBUFFER pTempBuffer; + //Todo:shall we check the pDSBufferDesc->dwBufferBytes with legal ranges between DSBSIZE_MIN and DSBSIZE_MAX again? HRESULT hRetDS = g_pDSound8->CreateSoundBuffer(pDSBufferDesc, &pTempBuffer, nullptr); - if (hRetDS == DS_OK) { - hRetDS = pTempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)&(pDSBuffer)); - pTempBuffer->Release(); - } + if (hRetDS == DS_OK) { + hRetDS = pTempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)&(pDSBuffer)); + pTempBuffer->Release(); + if (pDSBuffer == nullptr) { + EmuLog(LOG_LEVEL::WARNING, "CreateSoundBuffer:QueryInterface Failed!"); + } + } + else { + EmuLog(LOG_LEVEL::WARNING, "CreateSoundBuffer Failed!"); + } return hRetDS; } @@ -456,13 +463,17 @@ static inline void DSoundBufferReCreate( xbox::CDirectSoundVoice* Xb_Voice) { - DSoundBufferCreate(&DSBufferDesc, pDSBufferNew); - - if (pDS3DBuffer != nullptr) { + HRESULT hretDS = DSoundBufferCreate(&DSBufferDesc, pDSBufferNew); + //create new DS3DBuffer from the new DSBuffer if the new DSBuffer is created successfully. + if (pDSBufferNew != nullptr) { DSound3DBufferCreate(pDSBufferNew, pDS3DBufferNew); - } + DSoundBufferTransferSettings(pDSBuffer, pDSBufferNew, pDS3DBuffer, pDS3DBufferNew, Xb_Voice);//Sanity checks inside. + } + else { + EmuLog(LOG_LEVEL::WARNING, "DSoundBufferReCreate Failed!"); + } - DSoundBufferTransferSettings(pDSBuffer, pDSBufferNew, pDS3DBuffer, pDS3DBufferNew, Xb_Voice); + } static inline void DSoundBufferRelease( @@ -503,8 +514,12 @@ static inline void DSoundBufferResizeSetSize( return; } - pThis->EmuBufferDesc.dwBufferBytes = Host_dwByteLength; - + if (Host_dwByteLength< DSBSIZE_MIN) { //the min. buffer bytes must be equal or greater then DSBSIZE_MIN + pThis->EmuBufferDesc.dwBufferBytes = DSBSIZE_MIN; + } + else { + pThis->EmuBufferDesc.dwBufferBytes = Host_dwByteLength; + } // NOTE: Test case JSRF, if we allow to re-alloc without checking allocated buffer size. // Then it is somehow binded to IDirectSound_SetPosition control for any allocated audio afterward. @@ -534,7 +549,7 @@ static inline void DSoundBufferResizeUpdate( DWORD Xb_dwByteLength) { xbox::EmuDirectSoundBuffer* pThis = pHybridThis->emuDSBuffer; - DSoundBufferResizeSetSize(pHybridThis, hRet, Xb_dwByteLength); + DSoundBufferResizeSetSize(pHybridThis, hRet, Xb_dwByteLength); hRet = pThis->EmuDirectSoundBuffer8->Lock(0, 0, &pThis->Host_lock.pLockPtr1, &pThis->Host_lock.dwLockBytes1, nullptr, nullptr, DSBLOCK_ENTIREBUFFER);