Skip to content

Instantly share code, notes, and snippets.

@cadaver
Created May 2, 2013 21:37
Show Gist options
  • Save cadaver/5505659 to your computer and use it in GitHub Desktop.
Save cadaver/5505659 to your computer and use it in GitHub Desktop.
Fix heap corruption in BlenderLoader.cpp
diff --git a/code/BlenderLoader.cpp b/code/BlenderLoader.cpp
index 3b9200b..b92cc98 100644
--- a/code/BlenderLoader.cpp
+++ b/code/BlenderLoader.cpp
@@ -644,14 +644,17 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co
// collect per-submesh numbers
std::map<int,size_t> per_mat;
+ std::map<int,size_t> per_mat_verts;
for (int i = 0; i < mesh->totface; ++i) {
const MFace& mf = mesh->mface[i];
per_mat[ mf.mat_nr ]++;
+ per_mat_verts[ mf.mat_nr ] += mf.v4?4:3;
}
for (int i = 0; i < mesh->totpoly; ++i) {
const MPoly& mp = mesh->mpoly[i];
per_mat[ mp.mat_nr ]++;
+ per_mat_verts[ mp.mat_nr ] += mp.totloop;
}
// ... and allocate the corresponding meshes
@@ -665,8 +668,8 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co
temp->push_back(new aiMesh());
aiMesh* out = temp->back();
- out->mVertices = new aiVector3D[it.second*4];
- out->mNormals = new aiVector3D[it.second*4];
+ out->mVertices = new aiVector3D[per_mat_verts[it.first]];
+ out->mNormals = new aiVector3D[per_mat_verts[it.first]];
//out->mNumFaces = 0
//out->mNumVertices = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment