Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit 400d4e5

Browse files
authored
Add overflow check for VBO reader for meshconvert (#231)
1 parent 8a05b89 commit 400d4e5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

UVAtlasTool/Mesh.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,13 +1398,25 @@ HRESULT Mesh::CreateFromVBO(const wchar_t* szFileName, std::unique_ptr<Mesh>& re
13981398
if (!result)
13991399
return E_OUTOFMEMORY;
14001400

1401+
const uint64_t vertSizeBytes = static_cast<uint64_t>(header.numVertices) * sizeof(vertex_t);
1402+
if (vertSizeBytes > UINT32_MAX)
1403+
{
1404+
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
1405+
}
1406+
1407+
const uint64_t indexSizeBytes = static_cast<uint64_t>(header.numIndices) * sizeof(uint16_t);
1408+
if (indexSizeBytes > UINT32_MAX)
1409+
{
1410+
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
1411+
}
1412+
14011413
// Read vertices/indices from VBO
14021414
std::unique_ptr<vertex_t[]> vb(new (std::nothrow) vertex_t[header.numVertices]);
14031415
std::unique_ptr<uint16_t[]> ib(new (std::nothrow) uint16_t[header.numIndices]);
14041416
if (!vb || !ib)
14051417
return E_OUTOFMEMORY;
14061418

1407-
const auto vertSize = static_cast<DWORD>(sizeof(vertex_t) * header.numVertices);
1419+
const auto vertSize = static_cast<DWORD>(vertSizeBytes);
14081420

14091421
if (!ReadFile(hFile.get(), vb.get(), vertSize, &bytesRead, nullptr))
14101422
{
@@ -1414,7 +1426,7 @@ HRESULT Mesh::CreateFromVBO(const wchar_t* szFileName, std::unique_ptr<Mesh>& re
14141426
if (bytesRead != vertSize)
14151427
return E_FAIL;
14161428

1417-
const auto indexSize = static_cast<DWORD>(sizeof(uint16_t) * header.numIndices);
1429+
const auto indexSize = static_cast<DWORD>(indexSizeBytes);
14181430

14191431
if (!ReadFile(hFile.get(), ib.get(), indexSize, &bytesRead, nullptr))
14201432
{

0 commit comments

Comments
 (0)