diff --git a/DDSTextureLoader/DDSTextureLoader11.cpp b/DDSTextureLoader/DDSTextureLoader11.cpp index a21ec97..d814f8f 100644 --- a/DDSTextureLoader/DDSTextureLoader11.cpp +++ b/DDSTextureLoader/DDSTextureLoader11.cpp @@ -147,6 +147,8 @@ namespace return E_POINTER; } + *bitSize = 0; + if (ddsDataSize > UINT32_MAX) { return E_FAIL; @@ -179,7 +181,7 @@ namespace (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // Must be long enough for both headers and magic value - if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) + if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10))) { return E_FAIL; } @@ -212,6 +214,8 @@ namespace return E_POINTER; } + *bitSize = 0; + // open the file #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) ScopedHandle hFile(safe_handle(CreateFile2(fileName, @@ -261,19 +265,21 @@ namespace } // read the data in - DWORD BytesRead = 0; + DWORD bytesRead = 0; if (!ReadFile(hFile.get(), ddsData.get(), fileInfo.EndOfFile.LowPart, - &BytesRead, + &bytesRead, nullptr )) { + ddsData.reset(); return HRESULT_FROM_WIN32(GetLastError()); } - if (BytesRead < fileInfo.EndOfFile.LowPart) + if (bytesRead < fileInfo.EndOfFile.LowPart) { + ddsData.reset(); return E_FAIL; } @@ -281,6 +287,7 @@ namespace auto dwMagicNumber = *reinterpret_cast(ddsData.get()); if (dwMagicNumber != DDS_MAGIC) { + ddsData.reset(); return E_FAIL; } @@ -290,6 +297,7 @@ namespace if (hdr->size != sizeof(DDS_HEADER) || hdr->ddspf.size != sizeof(DDS_PIXELFORMAT)) { + ddsData.reset(); return E_FAIL; } @@ -299,8 +307,9 @@ namespace (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // Must be long enough for both headers and magic value - if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) + if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10))) { + ddsData.reset(); return E_FAIL; } diff --git a/DDSTextureLoader/DDSTextureLoader12.cpp b/DDSTextureLoader/DDSTextureLoader12.cpp index ea8dd72..9f1452f 100644 --- a/DDSTextureLoader/DDSTextureLoader12.cpp +++ b/DDSTextureLoader/DDSTextureLoader12.cpp @@ -170,6 +170,8 @@ namespace return E_POINTER; } + *bitSize = 0; + if (ddsDataSize > UINT32_MAX) { return E_FAIL; @@ -202,7 +204,7 @@ namespace (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // Must be long enough for both headers and magic value - if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) + if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10))) { return E_FAIL; } @@ -235,6 +237,8 @@ namespace return E_POINTER; } + *bitSize = 0; + // open the file ScopedHandle hFile(safe_handle(CreateFile2(fileName, GENERIC_READ, @@ -261,7 +265,7 @@ namespace } // Need at least enough data to fill the header and magic number to be a valid DDS - if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t))) + if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER))) { return E_FAIL; } @@ -274,19 +278,21 @@ namespace } // read the data in - DWORD BytesRead = 0; + DWORD bytesRead = 0; if (!ReadFile(hFile.get(), ddsData.get(), fileInfo.EndOfFile.LowPart, - &BytesRead, + &bytesRead, nullptr )) { + ddsData.reset(); return HRESULT_FROM_WIN32(GetLastError()); } - if (BytesRead < fileInfo.EndOfFile.LowPart) + if (bytesRead < fileInfo.EndOfFile.LowPart) { + ddsData.reset(); return E_FAIL; } @@ -294,6 +300,7 @@ namespace auto dwMagicNumber = *reinterpret_cast(ddsData.get()); if (dwMagicNumber != DDS_MAGIC) { + ddsData.reset(); return E_FAIL; } @@ -303,6 +310,7 @@ namespace if (hdr->size != sizeof(DDS_HEADER) || hdr->ddspf.size != sizeof(DDS_PIXELFORMAT)) { + ddsData.reset(); return E_FAIL; } @@ -312,8 +320,9 @@ namespace (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // Must be long enough for both headers and magic value - if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) + if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10))) { + ddsData.reset(); return E_FAIL; } diff --git a/DDSTextureLoader/DDSTextureLoader9.cpp b/DDSTextureLoader/DDSTextureLoader9.cpp index ee0f23f..28ce4a2 100644 --- a/DDSTextureLoader/DDSTextureLoader9.cpp +++ b/DDSTextureLoader/DDSTextureLoader9.cpp @@ -127,6 +127,8 @@ namespace return E_POINTER; } + *bitSize = 0; + if (ddsDataSize > UINT32_MAX) { return E_FAIL; @@ -184,6 +186,8 @@ namespace return E_POINTER; } + *bitSize = 0; + // open the file #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) ScopedHandle hFile(safe_handle(CreateFile2(fileName, @@ -233,19 +237,21 @@ namespace } // read the data in - DWORD BytesRead = 0; + DWORD bytesRead = 0; if (!ReadFile(hFile.get(), ddsData.get(), fileInfo.EndOfFile.LowPart, - &BytesRead, + &bytesRead, nullptr )) { + ddsData.reset(); return HRESULT_FROM_WIN32(GetLastError()); } - if (BytesRead < fileInfo.EndOfFile.LowPart) + if (bytesRead < fileInfo.EndOfFile.LowPart) { + ddsData.reset(); return E_FAIL; } @@ -253,6 +259,7 @@ namespace auto dwMagicNumber = *reinterpret_cast(ddsData.get()); if (dwMagicNumber != DDS_MAGIC) { + ddsData.reset(); return E_FAIL; } @@ -262,6 +269,7 @@ namespace if (hdr->size != sizeof(DDS_HEADER) || hdr->ddspf.size != sizeof(DDS_PIXELFORMAT)) { + ddsData.reset(); return E_FAIL; } @@ -270,6 +278,7 @@ namespace (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // We don't support the new DX10 header for Direct3D 9 + ddsData.reset(); return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); }