REVIEWED: Avoid realloc() calls, small security improvement

This commit is contained in:
Ray
2025-09-01 20:37:23 +02:00
parent 6226abb0d3
commit 1777da9056
4 changed files with 39 additions and 25 deletions

View File

@@ -6573,13 +6573,23 @@ static Model LoadM3D(const char *fileName)
// Materials are grouped together
if (mi != m3d->face[i].materialid)
{
// there should be only one material switch per material kind, but be bulletproof for non-optimal model files
// There should be only one material switch per material kind,
// but be bulletproof for non-optimal model files
if (k + 1 >= model.meshCount)
{
model.meshCount++;
model.meshes = (Mesh *)RL_REALLOC(model.meshes, model.meshCount*sizeof(Mesh));
memset(&model.meshes[model.meshCount - 1], 0, sizeof(Mesh));
model.meshMaterial = (int *)RL_REALLOC(model.meshMaterial, model.meshCount*sizeof(int));
// Create a second buffer for mesh re-allocation
Mesh *tempMeshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
memcpy(tempMeshes, model.meshes, (model.meshCount - 1)*sizeof(Mesh));
RL_FREE(model.meshes);
model.meshes = tempMeshes;
// Create a second buffer for material re-allocation
int *tempMeshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
memcpy(tempMeshMaterial, model.meshMaterial, (model.meshCount - 1)*sizeof(int));
RL_FREE(model.meshMaterial);
model.meshMaterial = tempMeshMaterial;
}
k++;