diff --git a/RecastDemo/Source/InputGeom.cpp b/RecastDemo/Source/InputGeom.cpp index 2a8300f..f1e0186 100644 --- a/RecastDemo/Source/InputGeom.cpp +++ b/RecastDemo/Source/InputGeom.cpp @@ -174,8 +174,12 @@ bool InputGeom::load(rcContext* ctx, const char* filePath) fclose(fp); return false; } - fread(buf, bufSize, 1, fp); + size_t readLen = fread(buf, bufSize, 1, fp); fclose(fp); + if (readLen != 1) + { + return false; + } m_offMeshConCount = 0; m_volumeCount = 0; diff --git a/RecastDemo/Source/MeshLoaderObj.cpp b/RecastDemo/Source/MeshLoaderObj.cpp index 1d7ca37..2ba8fd8 100644 --- a/RecastDemo/Source/MeshLoaderObj.cpp +++ b/RecastDemo/Source/MeshLoaderObj.cpp @@ -150,9 +150,14 @@ bool rcMeshLoaderObj::load(const char* filename) fclose(fp); return false; } - fread(buf, bufSize, 1, fp); + size_t readLen = fread(buf, bufSize, 1, fp); fclose(fp); + if (readLen != 1) + { + return false; + } + char* src = buf; char* srcEnd = buf + bufSize; char row[512]; diff --git a/RecastDemo/Source/SampleInterfaces.cpp b/RecastDemo/Source/SampleInterfaces.cpp index b6e64ee..75d5f06 100644 --- a/RecastDemo/Source/SampleInterfaces.cpp +++ b/RecastDemo/Source/SampleInterfaces.cpp @@ -312,8 +312,8 @@ bool FileIO::write(const void* ptr, const size_t size) bool FileIO::read(void* ptr, const size_t size) { if (!m_fp || m_mode != 2) return false; - fread(ptr, size, 1, m_fp); - return true; + size_t readLen = fread(ptr, size, 1, m_fp); + return readLen == 1; } diff --git a/RecastDemo/Source/Sample_TileMesh.cpp b/RecastDemo/Source/Sample_TileMesh.cpp index 8c7a36c..3c75dba 100644 --- a/RecastDemo/Source/Sample_TileMesh.cpp +++ b/RecastDemo/Source/Sample_TileMesh.cpp @@ -285,7 +285,12 @@ dtNavMesh* Sample_TileMesh::loadAll(const char* path) // Read header. NavMeshSetHeader header; - fread(&header, sizeof(NavMeshSetHeader), 1, fp); + size_t readLen = fread(&header, sizeof(NavMeshSetHeader), 1, fp); + if (readLen != 1) + { + fclose(fp); + return 0; + } if (header.magic != NAVMESHSET_MAGIC) { fclose(fp); @@ -314,15 +319,20 @@ dtNavMesh* Sample_TileMesh::loadAll(const char* path) for (int i = 0; i < header.numTiles; ++i) { NavMeshTileHeader tileHeader; - fread(&tileHeader, sizeof(tileHeader), 1, fp); + readLen = fread(&tileHeader, sizeof(tileHeader), 1, fp); + if (readLen != 1) + return 0; + if (!tileHeader.tileRef || !tileHeader.dataSize) break; unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM); if (!data) break; memset(data, 0, tileHeader.dataSize); - fread(data, tileHeader.dataSize, 1, fp); - + readLen = fread(data, tileHeader.dataSize, 1, fp); + if (readLen != 1) + return 0; + mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0); } diff --git a/RecastDemo/Source/TestCase.cpp b/RecastDemo/Source/TestCase.cpp index 7d4052f..be72d24 100644 --- a/RecastDemo/Source/TestCase.cpp +++ b/RecastDemo/Source/TestCase.cpp @@ -106,8 +106,12 @@ bool TestCase::load(const char* filePath) fclose(fp); return false; } - fread(buf, bufSize, 1, fp); + size_t readLen = fread(buf, bufSize, 1, fp); fclose(fp); + if (readLen != 1) + { + return false; + } char* src = buf; char* srcEnd = buf + bufSize; diff --git a/RecastDemo/Source/imguiRenderGL.cpp b/RecastDemo/Source/imguiRenderGL.cpp index 2dddffe..2c4e375 100644 --- a/RecastDemo/Source/imguiRenderGL.cpp +++ b/RecastDemo/Source/imguiRenderGL.cpp @@ -247,7 +247,7 @@ bool imguiRenderGLInit(const char* fontpath) FILE* fp = fopen(fontpath, "rb"); if (!fp) return false; fseek(fp, 0, SEEK_END); - int size = ftell(fp); + size_t size = ftell(fp); fseek(fp, 0, SEEK_SET); unsigned char* ttfBuffer = (unsigned char*)malloc(size); @@ -257,8 +257,13 @@ bool imguiRenderGLInit(const char* fontpath) return false; } - fread(ttfBuffer, 1, size, fp); + size_t readLen = fread(ttfBuffer, 1, size, fp); fclose(fp); + if (readLen != size) + { + return false; + } + fp = 0; unsigned char* bmap = (unsigned char*)malloc(512*512);