From 0b3f2b1765b8dd53c707ce38b1eca3f5f79c8a88 Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Tue, 30 Apr 2019 14:48:48 -0700 Subject: [PATCH] Fix crash due to invalid indices in baker::calculateNormals --- .../model-baker/CalculateBlendshapeNormalsTask.cpp | 2 +- .../src/model-baker/CalculateMeshNormalsTask.cpp | 2 +- libraries/model-baker/src/model-baker/ModelMath.h | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libraries/model-baker/src/model-baker/CalculateBlendshapeNormalsTask.cpp b/libraries/model-baker/src/model-baker/CalculateBlendshapeNormalsTask.cpp index b908fa9ced..e09bf7a419 100644 --- a/libraries/model-baker/src/model-baker/CalculateBlendshapeNormalsTask.cpp +++ b/libraries/model-baker/src/model-baker/CalculateBlendshapeNormalsTask.cpp @@ -61,7 +61,7 @@ void CalculateBlendshapeNormalsTask::run(const baker::BakeContextPointer& contex outVertex = blendshape.vertices[lookupIndex]; } else { // Index isn't in the blendshape, so return vertex from mesh - outVertex = mesh.vertices[lookupIndex]; + outVertex = baker::safeGet(mesh.vertices, lookupIndex); } }); } diff --git a/libraries/model-baker/src/model-baker/CalculateMeshNormalsTask.cpp b/libraries/model-baker/src/model-baker/CalculateMeshNormalsTask.cpp index a6884e104d..f07fb6fcd1 100644 --- a/libraries/model-baker/src/model-baker/CalculateMeshNormalsTask.cpp +++ b/libraries/model-baker/src/model-baker/CalculateMeshNormalsTask.cpp @@ -32,7 +32,7 @@ void CalculateMeshNormalsTask::run(const baker::BakeContextPointer& context, con return &normalsOut[normalIndex]; }, [&mesh](int vertexIndex, glm::vec3& outVertex) /* VertexSetter */ { - outVertex = mesh.vertices[vertexIndex]; + outVertex = baker::safeGet(mesh.vertices, vertexIndex); } ); } diff --git a/libraries/model-baker/src/model-baker/ModelMath.h b/libraries/model-baker/src/model-baker/ModelMath.h index 38bb3e1b3d..bbf42ffd9c 100644 --- a/libraries/model-baker/src/model-baker/ModelMath.h +++ b/libraries/model-baker/src/model-baker/ModelMath.h @@ -25,6 +25,17 @@ namespace baker { } } + template + const T& safeGet(const QVector& data, int i) { + static T t; + + if (i >= 0 && data.size() > i) { + return data[i]; + } else { + return t; + } + } + // Returns a reference to the normal at the specified index, or nullptr if it cannot be accessed using NormalAccessor = std::function;