diff --git a/DDSTextureLoader/DDSTextureLoader.cpp b/DDSTextureLoader/DDSTextureLoader.cpp index fcff7e6..07de0bb 100644 --- a/DDSTextureLoader/DDSTextureLoader.cpp +++ b/DDSTextureLoader/DDSTextureLoader.cpp @@ -851,13 +851,10 @@ namespace size_t d = depth; for (size_t i = 0; i < mipCount; i++) { - GetSurfaceInfo(w, - h, - format, - &NumBytes, - &RowBytes, - nullptr - ); + GetSurfaceInfo(w, h, format, &NumBytes, &RowBytes, nullptr); + + if (NumBytes > UINT32_MAX || RowBytes > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); if ((mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize)) { @@ -959,9 +956,9 @@ namespace initData, &tex ); - if (SUCCEEDED(hr) && tex != 0) + if (SUCCEEDED(hr) && tex) { - if (textureView != 0) + if (textureView) { D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; SRVDesc.Format = format; @@ -989,7 +986,7 @@ namespace } } - if (texture != 0) + if (texture) { *texture = tex; } @@ -1029,9 +1026,9 @@ namespace initData, &tex ); - if (SUCCEEDED(hr) && tex != 0) + if (SUCCEEDED(hr) && tex) { - if (textureView != 0) + if (textureView) { D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; SRVDesc.Format = format; @@ -1075,7 +1072,7 @@ namespace } } - if (texture != 0) + if (texture) { *texture = tex; } @@ -1106,9 +1103,9 @@ namespace initData, &tex ); - if (SUCCEEDED(hr) && tex != 0) + if (SUCCEEDED(hr) && tex) { - if (textureView != 0) + if (textureView) { D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; SRVDesc.Format = format; @@ -1127,7 +1124,7 @@ namespace } } - if (texture != 0) + if (texture) { *texture = tex; } @@ -1308,8 +1305,8 @@ namespace } } else if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || - (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) || - (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) + (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) || + (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) { return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } @@ -1330,7 +1327,7 @@ namespace } bool autogen = false; - if (mipCount == 1 && d3dContext != 0 && textureView != 0) // Must have context and shader-view to auto generate mipmaps + if (mipCount == 1 && d3dContext && textureView) // Must have context and shader-view to auto generate mipmaps { // See if format is supported for auto-gen mipmaps (varies by feature level) UINT fmtSupport = 0; @@ -1370,6 +1367,9 @@ namespace return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF); } + if (numBytes > UINT32_MAX || rowBytes > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + D3D11_SHADER_RESOURCE_VIEW_DESC desc; (*textureView)->GetDesc(&desc); @@ -1653,12 +1653,12 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice, texture, textureView); if (SUCCEEDED(hr)) { - if (texture != 0 && *texture != 0) + if (texture && *texture) { SetDebugObjectName(*texture, "DDSTextureLoader"); } - if (textureView != 0 && *textureView != 0) + if (textureView && *textureView) { SetDebugObjectName(*textureView, "DDSTextureLoader"); } @@ -1772,7 +1772,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice, if (SUCCEEDED(hr)) { #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) - if (texture != 0 || textureView != 0) + if (texture || textureView) { CHAR strFileA[MAX_PATH]; int result = WideCharToMultiByte(CP_ACP, @@ -1796,7 +1796,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice, pstrName++; } - if (texture != 0 && *texture != 0) + if (texture && *texture) { (*texture)->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast(strnlen_s(pstrName, MAX_PATH)), @@ -1804,7 +1804,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice, ); } - if (textureView != 0 && *textureView != 0) + if (textureView && *textureView) { (*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast(strnlen_s(pstrName, MAX_PATH)), diff --git a/DDSTextureLoader/DDSTextureLoader12.cpp b/DDSTextureLoader/DDSTextureLoader12.cpp index 354018c..813193b 100644 --- a/DDSTextureLoader/DDSTextureLoader12.cpp +++ b/DDSTextureLoader/DDSTextureLoader12.cpp @@ -930,13 +930,10 @@ namespace size_t d = depth; for (size_t i = 0; i < mipCount; i++) { - GetSurfaceInfo(w, - h, - format, - &NumBytes, - &RowBytes, - nullptr - ); + GetSurfaceInfo(w, h, format, &NumBytes, &RowBytes, nullptr); + + if (NumBytes > UINT32_MAX || RowBytes > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); if ((mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize)) { @@ -1040,7 +1037,7 @@ namespace IID_PPV_ARGS(texture)); if (SUCCEEDED(hr)) { - _Analysis_assume_(*texture != 0); + _Analysis_assume_(*texture != nullptr); SetDebugObjectName(*texture, L"DDSTextureLoader"); } @@ -1207,8 +1204,8 @@ namespace } } else if ((arraySize > D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || - (width > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION) || - (height > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION)) + (width > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION) || + (height > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION)) { return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } @@ -1434,7 +1431,7 @@ HRESULT DirectX::LoadDDSTextureFromMemoryEx( texture, subresources, isCubeMap); if (SUCCEEDED(hr)) { - if (texture != 0 && *texture != 0) + if (texture && *texture) { SetDebugObjectName(*texture, L"DDSTextureLoader"); } @@ -1526,7 +1523,7 @@ HRESULT DirectX::LoadDDSTextureFromFileEx( if (SUCCEEDED(hr)) { #if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) - if (texture != 0) + if (texture) { CHAR strFileA[MAX_PATH]; int result = WideCharToMultiByte(CP_ACP, @@ -1550,7 +1547,7 @@ HRESULT DirectX::LoadDDSTextureFromFileEx( pstrName++; } - if (texture != 0 && *texture != 0) + if (texture && *texture) { (*texture)->SetName(pstrName); } diff --git a/ScreenGrab/ScreenGrab.cpp b/ScreenGrab/ScreenGrab.cpp index 9413e7e..71099f4 100644 --- a/ScreenGrab/ScreenGrab.cpp +++ b/ScreenGrab/ScreenGrab.cpp @@ -629,7 +629,7 @@ namespace desc.SampleDesc.Quality = 0; ComPtr pTemp; - hr = d3dDevice->CreateTexture2D( &desc, 0, pTemp.GetAddressOf() ); + hr = d3dDevice->CreateTexture2D( &desc, nullptr, pTemp.GetAddressOf() ); if ( FAILED(hr) ) return hr; @@ -659,7 +659,7 @@ namespace desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.Usage = D3D11_USAGE_STAGING; - hr = d3dDevice->CreateTexture2D(&desc, 0, pStaging.ReleaseAndGetAddressOf()); + hr = d3dDevice->CreateTexture2D(&desc, nullptr, pStaging.ReleaseAndGetAddressOf()); if ( FAILED(hr) ) return hr; @@ -680,7 +680,7 @@ namespace desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.Usage = D3D11_USAGE_STAGING; - hr = d3dDevice->CreateTexture2D(&desc, 0, pStaging.ReleaseAndGetAddressOf()); + hr = d3dDevice->CreateTexture2D(&desc, nullptr, pStaging.ReleaseAndGetAddressOf()); if ( FAILED(hr) ) return hr; @@ -745,9 +745,11 @@ namespace //-------------------------------------------------------------------------------------- -HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, - _In_ ID3D11Resource* pSource, - _In_z_ const wchar_t* fileName ) +_Use_decl_annotations_ +HRESULT DirectX::SaveDDSTextureToFile( + ID3D11DeviceContext* pContext, + ID3D11Resource* pSource, + const wchar_t* fileName ) { if ( !fileName ) return E_INVALIDARG; @@ -834,7 +836,7 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, memcpy_s( &header->ddspf, sizeof(header->ddspf), &DDSPF_DX10, sizeof(DDS_PIXELFORMAT) ); headerSize += sizeof(DDS_HEADER_DXT10); - extHeader = reinterpret_cast( reinterpret_cast(&fileHeader[0]) + sizeof(uint32_t) + sizeof(DDS_HEADER) ); + extHeader = reinterpret_cast( fileHeader + sizeof(uint32_t) + sizeof(DDS_HEADER) ); memset( extHeader, 0, sizeof(DDS_HEADER_DXT10) ); extHeader->dxgiFormat = desc.Format; extHeader->resourceDimension = D3D11_RESOURCE_DIMENSION_TEXTURE2D; @@ -845,6 +847,9 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, size_t rowPitch, slicePitch, rowCount; GetSurfaceInfo( desc.Width, desc.Height, desc.Format, &slicePitch, &rowPitch, &rowCount ); + if (rowPitch > UINT32_MAX || slicePitch > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + if ( IsCompressed( desc.Format ) ) { header->flags |= DDS_HEADER_FLAGS_LINEARSIZE; @@ -866,7 +871,7 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, if ( FAILED(hr) ) return hr; - auto sptr = reinterpret_cast( mapped.pData ); + auto sptr = static_cast( mapped.pData ); if ( !sptr ) { pContext->Unmap( pStaging.Get(), 0 ); @@ -905,12 +910,14 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, } //-------------------------------------------------------------------------------------- -HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext, - _In_ ID3D11Resource* pSource, - _In_ REFGUID guidContainerFormat, - _In_z_ const wchar_t* fileName, - _In_opt_ const GUID* targetFormat, - _In_opt_ std::function setCustomProps ) +_Use_decl_annotations_ +HRESULT DirectX::SaveWICTextureToFile( + ID3D11DeviceContext* pContext, + ID3D11Resource* pSource, + REFGUID guidContainerFormat, + const wchar_t* fileName, + const GUID* targetFormat, + std::function setCustomProps ) { if ( !fileName ) return E_INVALIDARG; @@ -1037,7 +1044,7 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext, } else { - // Screenshots don’t typically include the alpha channel of the render target + // Screenshots don't typically include the alpha channel of the render target switch ( desc.Format ) { #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) @@ -1141,7 +1148,7 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext, ComPtr source; hr = pWIC->CreateBitmapFromMemory( desc.Width, desc.Height, pfGuid, mapped.RowPitch, mapped.RowPitch * desc.Height, - reinterpret_cast( mapped.pData ), source.GetAddressOf() ); + static_cast( mapped.pData ), source.GetAddressOf() ); if ( FAILED(hr) ) { pContext->Unmap( pStaging.Get(), 0 ); @@ -1181,7 +1188,7 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext, else { // No conversion required - hr = frame->WritePixels( desc.Height, mapped.RowPitch, mapped.RowPitch * desc.Height, reinterpret_cast( mapped.pData ) ); + hr = frame->WritePixels( desc.Height, mapped.RowPitch, mapped.RowPitch * desc.Height, static_cast( mapped.pData ) ); if ( FAILED(hr) ) return hr; } diff --git a/ScreenGrab/ScreenGrab12.cpp b/ScreenGrab/ScreenGrab12.cpp index d26a0f9..4fed21b 100644 --- a/ScreenGrab/ScreenGrab12.cpp +++ b/ScreenGrab/ScreenGrab12.cpp @@ -601,8 +601,8 @@ namespace _In_ D3D12_RESOURCE_STATES stateBefore, _In_ D3D12_RESOURCE_STATES stateAfter) { - assert(commandList != 0); - assert(resource != 0); + assert(commandList != nullptr); + assert(resource != nullptr); if (stateBefore == stateAfter) return; @@ -634,6 +634,9 @@ namespace if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); + if (srcPitch > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + UINT numberOfPlanes = D3D12GetFormatPlaneCount(device, desc.Format); if (numberOfPlanes != 1) return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); @@ -711,7 +714,7 @@ namespace DXGI_FORMAT fmt = EnsureNotTypeless(desc.Format); - D3D12_FEATURE_DATA_FORMAT_SUPPORT formatInfo = { fmt }; + D3D12_FEATURE_DATA_FORMAT_SUPPORT formatInfo = { fmt, D3D12_FORMAT_SUPPORT1_NONE, D3D12_FORMAT_SUPPORT2_NONE }; hr = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &formatInfo, sizeof(formatInfo)); if (FAILED(hr)) return hr; @@ -769,7 +772,7 @@ namespace return hr; // Execute the command list - pCommandQ->ExecuteCommandLists(1, (ID3D12CommandList**)commandList.GetAddressOf()); + pCommandQ->ExecuteCommandLists(1, CommandListCast(commandList.GetAddressOf())); // Signal the fence hr = pCommandQ->Signal(fence.Get(), 1); @@ -806,11 +809,12 @@ namespace //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, - ID3D12Resource* pSource, - const wchar_t* fileName, - D3D12_RESOURCE_STATES beforeState, - D3D12_RESOURCE_STATES afterState) +HRESULT DirectX::SaveDDSTextureToFile( + ID3D12CommandQueue* pCommandQ, + ID3D12Resource* pSource, + const wchar_t* fileName, + D3D12_RESOURCE_STATES beforeState, + D3D12_RESOURCE_STATES afterState) { if ( !fileName ) return E_INVALIDARG; @@ -819,7 +823,11 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, pCommandQ->GetDevice(IID_PPV_ARGS(device.GetAddressOf())); // Get the size of the image - D3D12_RESOURCE_DESC desc = pSource->GetDesc(); + const auto desc = pSource->GetDesc(); + + if (desc.Width > UINT32_MAX) + return E_INVALIDARG; + UINT64 totalResourceSize = 0; UINT64 fpRowPitch = 0; UINT fpRowCount = 0; @@ -837,6 +845,9 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, // Round up the srcPitch to multiples of 256 UINT64 dstRowPitch = (fpRowPitch + 255) & ~0xFF; + if (dstRowPitch > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + ComPtr pStaging; HRESULT hr = CaptureTexture( device.Get(), pCommandQ, pSource, dstRowPitch, desc, pStaging, beforeState, afterState ); if ( FAILED(hr) ) @@ -861,7 +872,7 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, header->size = sizeof( DDS_HEADER ); header->flags = DDS_HEADER_FLAGS_TEXTURE | DDS_HEADER_FLAGS_MIPMAP; header->height = desc.Height; - header->width = (uint32_t) desc.Width; + header->width = static_cast(desc.Width); header->mipMapCount = 1; header->caps = DDS_SURFACE_FLAGS_TEXTURE; @@ -914,7 +925,7 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, memcpy_s( &header->ddspf, sizeof(header->ddspf), &DDSPF_DX10, sizeof(DDS_PIXELFORMAT) ); headerSize += sizeof(DDS_HEADER_DXT10); - extHeader = reinterpret_cast( reinterpret_cast(&fileHeader[0]) + sizeof(uint32_t) + sizeof(DDS_HEADER) ); + extHeader = reinterpret_cast(fileHeader + sizeof(uint32_t) + sizeof(DDS_HEADER) ); memset( extHeader, 0, sizeof(DDS_HEADER_DXT10) ); extHeader->dxgiFormat = desc.Format; extHeader->resourceDimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; @@ -923,7 +934,10 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, } size_t rowPitch, slicePitch, rowCount; - GetSurfaceInfo( (size_t)desc.Width, desc.Height, desc.Format, &slicePitch, &rowPitch, &rowCount ); + GetSurfaceInfo(static_cast(desc.Width), desc.Height, desc.Format, &slicePitch, &rowPitch, &rowCount); + + if (rowPitch > UINT32_MAX || slicePitch > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); if ( IsCompressed( desc.Format ) ) { @@ -944,14 +958,18 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, assert(fpRowCount == rowCount); assert(fpRowPitch == rowPitch); - void* pMappedMemory; - D3D12_RANGE readRange = { 0, static_cast(dstRowPitch * rowCount) }; + UINT64 imageSize = dstRowPitch * UINT64(rowCount); + if (imageSize > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + + void* pMappedMemory = nullptr; + D3D12_RANGE readRange = { 0, static_cast(imageSize) }; D3D12_RANGE writeRange = { 0, 0 }; hr = pStaging->Map(0, &readRange, &pMappedMemory ); if ( FAILED(hr) ) return hr; - auto sptr = reinterpret_cast(pMappedMemory); + auto sptr = static_cast(pMappedMemory); if ( !sptr ) { pStaging->Unmap(0, &writeRange); @@ -991,14 +1009,15 @@ HRESULT DirectX::SaveDDSTextureToFile( ID3D12CommandQueue* pCommandQ, //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, - ID3D12Resource* pSource, - REFGUID guidContainerFormat, - const wchar_t* fileName, - D3D12_RESOURCE_STATES beforeState, - D3D12_RESOURCE_STATES afterState, - const GUID* targetFormat, - std::function setCustomProps ) +HRESULT DirectX::SaveWICTextureToFile( + ID3D12CommandQueue* pCommandQ, + ID3D12Resource* pSource, + REFGUID guidContainerFormat, + const wchar_t* fileName, + D3D12_RESOURCE_STATES beforeState, + D3D12_RESOURCE_STATES afterState, + const GUID* targetFormat, + std::function setCustomProps ) { if ( !fileName ) return E_INVALIDARG; @@ -1007,7 +1026,11 @@ HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, pCommandQ->GetDevice(IID_PPV_ARGS(device.GetAddressOf())); // Get the size of the image - D3D12_RESOURCE_DESC desc = pSource->GetDesc(); + const auto desc = pSource->GetDesc(); + + if (desc.Width > UINT32_MAX) + return E_INVALIDARG; + UINT64 totalResourceSize = 0; UINT64 fpRowPitch = 0; UINT fpRowCount = 0; @@ -1025,6 +1048,9 @@ HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, // Round up the srcPitch to multiples of 256 UINT64 dstRowPitch = (fpRowPitch + 255) & ~0xFF; + if (dstRowPitch > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + ComPtr pStaging; HRESULT hr = CaptureTexture(device.Get(), pCommandQ, pSource, dstRowPitch, desc, pStaging, beforeState, afterState); if (FAILED(hr)) @@ -1146,7 +1172,7 @@ HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, } else { - // Screenshots don’t typically include the alpha channel of the render target + // Screenshots don't typically include the alpha channel of the render target switch ( desc.Format ) { case DXGI_FORMAT_R32G32B32A32_FLOAT: @@ -1230,8 +1256,12 @@ HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, } } - void* pMappedMemory; - D3D12_RANGE readRange = {0, static_cast(dstRowPitch * desc.Height)}; + UINT64 imageSize = dstRowPitch * UINT64(desc.Height); + if (imageSize > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + + void* pMappedMemory = nullptr; + D3D12_RANGE readRange = {0, static_cast(imageSize)}; D3D12_RANGE writeRange = {0, 0}; hr = pStaging->Map(0, &readRange, &pMappedMemory); if (FAILED(hr)) @@ -1242,8 +1272,8 @@ HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, // Conversion required to write ComPtr source; hr = pWIC->CreateBitmapFromMemory(static_cast(desc.Width), desc.Height, pfGuid, - static_cast(dstRowPitch), static_cast(dstRowPitch * desc.Height), - reinterpret_cast(pMappedMemory), source.GetAddressOf() ); + static_cast(dstRowPitch), static_cast(imageSize), + static_cast(pMappedMemory), source.GetAddressOf() ); if ( FAILED(hr) ) { pStaging->Unmap( 0, &writeRange ); @@ -1283,7 +1313,7 @@ HRESULT DirectX::SaveWICTextureToFile( ID3D12CommandQueue* pCommandQ, else { // No conversion required - hr = frame->WritePixels( desc.Height, static_cast(dstRowPitch), static_cast(dstRowPitch * desc.Height), reinterpret_cast( pMappedMemory ) ); + hr = frame->WritePixels( desc.Height, static_cast(dstRowPitch), static_cast(imageSize), static_cast( pMappedMemory ) ); if ( FAILED(hr) ) return hr; } diff --git a/WICTextureLoader/WICTextureLoader.cpp b/WICTextureLoader/WICTextureLoader.cpp index 8002a05..2d2a6ad 100644 --- a/WICTextureLoader/WICTextureLoader.cpp +++ b/WICTextureLoader/WICTextureLoader.cpp @@ -311,6 +311,9 @@ namespace if (FAILED(hr)) return hr; + if (maxsize > UINT32_MAX) + return E_INVALIDARG; + assert(width > 0 && height > 0); if (!maxsize) @@ -421,7 +424,7 @@ namespace } #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) - if ((format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0) + if ((format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext && textureView) { // Special case test for optional device support for autogen mipchains for R32G32B32_FLOAT UINT fmtSupport = 0; @@ -492,8 +495,14 @@ namespace } // Allocate temporary memory for image - size_t rowPitch = (twidth * bpp + 7) / 8; - size_t imageSize = rowPitch * theight; + uint64_t rowBytes = (uint64_t(twidth) * uint64_t(bpp) + 7u) / 8u; + uint64_t numBytes = rowBytes * uint64_t(height); + + if (rowBytes > UINT32_MAX || numBytes > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + + auto rowPitch = static_cast(rowBytes); + auto imageSize = static_cast(numBytes); std::unique_ptr temp(new (std::nothrow) uint8_t[imageSize]); if (!temp) @@ -590,7 +599,7 @@ namespace // See if format is supported for auto-gen mipmaps (varies by feature level) bool autogen = false; - if (d3dContext != 0 && textureView != 0) // Must have context and shader-view to auto generate mipmaps + if (d3dContext && textureView) // Must have context and shader-view to auto generate mipmaps { UINT fmtSupport = 0; hr = d3dDevice->CheckFormatSupport(format, &fmtSupport); @@ -630,9 +639,9 @@ namespace ID3D11Texture2D* tex = nullptr; hr = d3dDevice->CreateTexture2D(&desc, (autogen) ? nullptr : &initData, &tex); - if (SUCCEEDED(hr) && tex != 0) + if (SUCCEEDED(hr) && tex) { - if (textureView != 0) + if (textureView) { D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; SRVDesc.Format = desc.Format; @@ -649,13 +658,13 @@ namespace if (autogen) { - assert(d3dContext != 0); + assert(d3dContext != nullptr); d3dContext->UpdateSubresource(tex, 0, nullptr, temp.get(), static_cast(rowPitch), static_cast(imageSize)); d3dContext->GenerateMips(*textureView); } } - if (texture != 0) + if (texture) { *texture = tex; } @@ -779,12 +788,12 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice, if (FAILED(hr)) return hr; - if (texture != 0 && *texture != 0) + if (texture && *texture) { SetDebugObjectName(*texture, "WICTextureLoader"); } - if (textureView != 0 && *textureView != 0) + if (textureView && *textureView) { SetDebugObjectName(*textureView, "WICTextureLoader"); } @@ -882,7 +891,7 @@ HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice, #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) if (SUCCEEDED(hr)) { - if (texture != 0 || textureView != 0) + if (texture || textureView) { char strFileA[MAX_PATH]; int result = WideCharToMultiByte(CP_ACP, @@ -906,7 +915,7 @@ HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice, pstrName++; } - if (texture != 0 && *texture != 0) + if (texture && *texture) { (*texture)->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast(strnlen_s(pstrName, MAX_PATH)), @@ -914,7 +923,7 @@ HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice, ); } - if (textureView != 0 && *textureView != 0) + if (textureView && *textureView) { (*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast(strnlen_s(pstrName, MAX_PATH)), diff --git a/WICTextureLoader/WICTextureLoader12.cpp b/WICTextureLoader/WICTextureLoader12.cpp index 4028550..1c5f90c 100644 --- a/WICTextureLoader/WICTextureLoader12.cpp +++ b/WICTextureLoader/WICTextureLoader12.cpp @@ -278,13 +278,14 @@ namespace assert(width > 0 && height > 0); + if (maxsize > UINT32_MAX) + return E_INVALIDARG; + if (!maxsize) { maxsize = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION; } - assert(maxsize > 0); - UINT twidth, theight; if (width > maxsize || height > maxsize) { @@ -386,8 +387,14 @@ namespace } // Allocate memory for decoded image - size_t rowPitch = (twidth * bpp + 7) / 8; - size_t imageSize = rowPitch * theight; + uint64_t rowBytes = (uint64_t(twidth) * uint64_t(bpp) + 7u) / 8u; + uint64_t numBytes = rowBytes * uint64_t(height); + + if (rowBytes > UINT32_MAX || numBytes > UINT32_MAX) + return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); + + auto rowPitch = static_cast(rowBytes); + auto imageSize = static_cast(numBytes); decodedData.reset(new (std::nothrow) uint8_t[imageSize]); if (!decodedData) @@ -489,7 +496,7 @@ namespace D3D12_RESOURCE_DESC desc = {}; desc.Width = twidth; desc.Height = theight; - desc.MipLevels = (uint16_t)mipCount; + desc.MipLevels = static_cast(mipCount); desc.DepthOrArraySize = 1; desc.Format = format; desc.SampleDesc.Count = 1; @@ -513,7 +520,7 @@ namespace return hr; } - _Analysis_assume_(tex != 0); + _Analysis_assume_(tex != nullptr); subresource.pData = decodedData.get(); subresource.RowPitch = rowPitch; @@ -689,7 +696,7 @@ HRESULT DirectX::LoadWICTextureFromFileEx( pstrName++; } - if (texture != 0 && *texture != 0) + if (texture && *texture) { (*texture)->SetName(pstrName); }