From 1a7bdd4440f9ed6fdecbc63b7abcb91fa1e3c0c6 Mon Sep 17 00:00:00 2001 From: Vincent Povirk Date: Fri, 9 Apr 2010 14:49:06 -0500 Subject: [PATCH 4/6] windowscodecs: Add locking to the ICO decoder. --- dlls/windowscodecs/icoformat.c | 60 ++++++++++++++++++++++++++++++++------- dlls/windowscodecs/regsvr.c | 2 +- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/dlls/windowscodecs/icoformat.c b/dlls/windowscodecs/icoformat.c index ab30ab0..e1ea7c6 100644 --- a/dlls/windowscodecs/icoformat.c +++ b/dlls/windowscodecs/icoformat.c @@ -62,6 +62,7 @@ typedef struct { BOOL initialized; IStream *stream; ICONHEADER header; + CRITICAL_SECTION lock; /* must be held when accessing stream */ } IcoDecoder; typedef struct { @@ -512,15 +513,17 @@ static HRESULT WINAPI IcoFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface, const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer) { IcoFrameDecode *This = (IcoFrameDecode*)iface; - HRESULT hr; + HRESULT hr=S_OK; UINT width, height, stride; TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer); + EnterCriticalSection(&This->parent->lock); if (!This->bits) { hr = IcoFrameDecode_ReadPixels(This); - if (FAILED(hr)) return hr; } + LeaveCriticalSection(&This->parent->lock); + if (FAILED(hr)) return hr; width = This->entry.bWidth ? This->entry.bWidth : 256; height = This->entry.bHeight ? This->entry.bHeight : 256; @@ -606,6 +609,8 @@ static ULONG WINAPI IcoDecoder_Release(IWICBitmapDecoder *iface) if (ref == 0) { + This->lock.DebugInfo->Spare[0] = 0; + DeleteCriticalSection(&This->lock); if (This->stream) IStream_Release(This->stream); HeapFree(GetProcessHeap(), 0, This); } @@ -629,23 +634,37 @@ static HRESULT WINAPI IcoDecoder_Initialize(IWICBitmapDecoder *iface, IStream *p ULONG bytesread; TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions); - if (This->initialized) return WINCODEC_ERR_WRONGSTATE; + EnterCriticalSection(&This->lock); + + if (This->initialized) + { + hr = WINCODEC_ERR_WRONGSTATE; + goto end; + } seek.QuadPart = 0; hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL); - if (FAILED(hr)) return hr; + if (FAILED(hr)) goto end; hr = IStream_Read(pIStream, &This->header, sizeof(ICONHEADER), &bytesread); - if (FAILED(hr)) return hr; + if (FAILED(hr)) goto end; if (bytesread != sizeof(ICONHEADER) || This->header.idReserved != 0 || - This->header.idType != 1) return E_FAIL; + This->header.idType != 1) + { + hr = E_FAIL; + goto end; + } This->initialized = TRUE; This->stream = pIStream; IStream_AddRef(pIStream); - return S_OK; +end: + + LeaveCriticalSection(&This->lock); + + return hr; } static HRESULT WINAPI IcoDecoder_GetContainerFormat(IWICBitmapDecoder *iface, @@ -715,18 +734,32 @@ static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface, UINT index, IWICBitmapFrameDecode **ppIBitmapFrame) { IcoDecoder *This = (IcoDecoder*)iface; - IcoFrameDecode *result; + IcoFrameDecode *result=NULL; LARGE_INTEGER seek; HRESULT hr; ULONG bytesread; TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame); - if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED; + EnterCriticalSection(&This->lock); + + if (!This->initialized) + { + hr = WINCODEC_ERR_NOTINITIALIZED; + goto fail; + } - if (This->header.idCount < index) return E_INVALIDARG; + if (This->header.idCount < index) + { + hr = E_INVALIDARG; + goto fail; + } result = HeapAlloc(GetProcessHeap(), 0, sizeof(IcoFrameDecode)); - if (!result) return E_OUTOFMEMORY; + if (!result) + { + hr = E_OUTOFMEMORY; + goto fail; + } result->lpVtbl = &IcoFrameDecode_Vtbl; result->ref = 1; @@ -745,9 +778,12 @@ static HRESULT WINAPI IcoDecoder_GetFrame(IWICBitmapDecoder *iface, *ppIBitmapFrame = (IWICBitmapFrameDecode*)result; + LeaveCriticalSection(&This->lock); + return S_OK; fail: + LeaveCriticalSection(&This->lock); HeapFree(GetProcessHeap(), 0, result); if (SUCCEEDED(hr)) hr = E_FAIL; TRACE("<-- %x\n", hr); @@ -789,6 +825,8 @@ HRESULT IcoDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv) This->ref = 1; This->stream = NULL; This->initialized = FALSE; + InitializeCriticalSection(&This->lock); + This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcoDecoder.lock"); ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv); IUnknown_Release((IUnknown*)This); diff --git a/dlls/windowscodecs/regsvr.c b/dlls/windowscodecs/regsvr.c index fa876c8..83d25ef 100644 --- a/dlls/windowscodecs/regsvr.c +++ b/dlls/windowscodecs/regsvr.c @@ -763,7 +763,7 @@ static struct regsvr_coclass const coclass_list[] = { "WIC ICO Decoder", NULL, "windowscodecs.dll", - "Apartment" + "Both" }, { &CLSID_WICJpegDecoder, "WIC JPEG Decoder", -- 1.6.3.3