From 05cf8bfb2ef1289b81a06907d540e78d74c5ee48 Mon Sep 17 00:00:00 2001 From: JeffH-BMG <37119778+JeffH-BMG@users.noreply.github.com> Date: Tue, 6 Mar 2018 13:48:11 -0500 Subject: [PATCH 1/4] Fix import of binary STL files in double-precision builds When ASSIMP_DOUBLE_PRECISION is used, the STL loader attempts to read 8-byte double vertex and normal values from the STL file. STL files are written using 4-byte floats, however, and the import will read past the end of the buffer, and possibly crash. --- code/STLLoader.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/code/STLLoader.cpp b/code/STLLoader.cpp index fc326b2c7..863f7affd 100644 --- a/code/STLLoader.cpp +++ b/code/STLLoader.cpp @@ -449,26 +449,29 @@ bool STLImporter::LoadBinaryFile() aiVector3D *vp = pMesh->mVertices = new aiVector3D[pMesh->mNumVertices]; aiVector3D *vn = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices]; + typedef aiVector3t aiVector3F; + aiVector3F* theVec; + for ( unsigned int i = 0; i < pMesh->mNumFaces; ++i ) { // NOTE: Blender sometimes writes empty normals ... this is not // our fault ... the RemoveInvalidData helper step should fix that - ::memcpy( vn, sz, sizeof( aiVector3D ) ); - sz += sizeof(aiVector3D); - *(vn+1) = *vn; - *(vn+2) = *vn; - vn += 3; - ::memcpy( vp, sz, sizeof( aiVector3D ) ); - ++vp; - sz += sizeof(aiVector3D); + // There's one normal for the face in the STL; use it three times + // for vertex normals + theVec = (aiVector3F*) sz; + *vn++ = *theVec; + *vn++ = *theVec; + *vn++ = *theVec++; - ::memcpy( vp, sz, sizeof( aiVector3D ) ); - ++vp; - sz += sizeof(aiVector3D); + // vertex 1 + *vp++ = *theVec++; - ::memcpy( vp, sz, sizeof( aiVector3D ) ); - ++vp; - sz += sizeof(aiVector3D); + // vertex 2 + *vp++ = *theVec++; + + // vertex 3 + *vp++ = *theVec; + sz += 4 * sizeof aiVector3F; uint16_t color = *((uint16_t*)sz); sz += 2; From 6fd64b95c32699649e6e392fb05bbf0af01cd685 Mon Sep 17 00:00:00 2001 From: JeffH-BMG <37119778+JeffH-BMG@users.noreply.github.com> Date: Tue, 6 Mar 2018 15:03:44 -0500 Subject: [PATCH 2/4] Fix compile error Add parens to use of 'sizeof' operator --- code/STLLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/STLLoader.cpp b/code/STLLoader.cpp index 863f7affd..115e5f816 100644 --- a/code/STLLoader.cpp +++ b/code/STLLoader.cpp @@ -471,7 +471,7 @@ bool STLImporter::LoadBinaryFile() // vertex 3 *vp++ = *theVec; - sz += 4 * sizeof aiVector3F; + sz += 4 * sizeof( aiVector3F ); uint16_t color = *((uint16_t*)sz); sz += 2; From 89a4cf94957711117e7c64d483bb1fe42d05f7e9 Mon Sep 17 00:00:00 2001 From: JeffH-BMG <37119778+JeffH-BMG@users.noreply.github.com> Date: Wed, 7 Mar 2018 17:26:01 -0500 Subject: [PATCH 3/4] Respond to comments Use memcpy() to read normals and vertices, to mitigate alignment issues, per comments. --- code/STLLoader.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/code/STLLoader.cpp b/code/STLLoader.cpp index 115e5f816..e01162a30 100644 --- a/code/STLLoader.cpp +++ b/code/STLLoader.cpp @@ -451,6 +451,7 @@ bool STLImporter::LoadBinaryFile() typedef aiVector3t aiVector3F; aiVector3F* theVec; + aiVector3F theVec3F; for ( unsigned int i = 0; i < pMesh->mNumFaces; ++i ) { // NOTE: Blender sometimes writes empty normals ... this is not @@ -459,19 +460,32 @@ bool STLImporter::LoadBinaryFile() // There's one normal for the face in the STL; use it three times // for vertex normals theVec = (aiVector3F*) sz; - *vn++ = *theVec; - *vn++ = *theVec; - *vn++ = *theVec++; + ::memcpy( &theVec3F, theVec, sizeof(aiVector3F) ); + vn->x = theVec3F.x; vn->y = theVec3F.y; vn->z = theVec3F.z; + *(vn+1) = *vn; + *(vn+2) = *vn; + ++theVec; + vn += 3; // vertex 1 - *vp++ = *theVec++; + ::memcpy( &theVec3F, theVec, sizeof(aiVector3F) ); + vp->x = theVec3F.x; vp->y = theVec3F.y; vp->z = theVec3F.z; + ++theVec; + ++vp; // vertex 2 - *vp++ = *theVec++; + ::memcpy( &theVec3F, theVec, sizeof(aiVector3F) ); + vp->x = theVec3F.x; vp->y = theVec3F.y; vp->z = theVec3F.z; + ++theVec; + ++vp; // vertex 3 - *vp++ = *theVec; - sz += 4 * sizeof( aiVector3F ); + ::memcpy( &theVec3F, theVec, sizeof(aiVector3F) ); + vp->x = theVec3F.x; vp->y = theVec3F.y; vp->z = theVec3F.z; + ++theVec; + ++vp; + + sz = theVec; uint16_t color = *((uint16_t*)sz); sz += 2; From cfd56a43a578f77a1c63a95ed28084a3ae3c4e6e Mon Sep 17 00:00:00 2001 From: JeffH-BMG <37119778+JeffH-BMG@users.noreply.github.com> Date: Wed, 7 Mar 2018 18:24:38 -0500 Subject: [PATCH 4/4] Fix compile error Fixed bad cast. --- code/STLLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/STLLoader.cpp b/code/STLLoader.cpp index e01162a30..3a85a5495 100644 --- a/code/STLLoader.cpp +++ b/code/STLLoader.cpp @@ -485,7 +485,7 @@ bool STLImporter::LoadBinaryFile() ++theVec; ++vp; - sz = theVec; + sz = (const unsigned char*) theVec; uint16_t color = *((uint16_t*)sz); sz += 2;