From 732ba41c73415ca3d293eb7b50dcdc3e48667566 Mon Sep 17 00:00:00 2001 From: tristan Date: Tue, 18 Dec 2018 10:13:03 +0100 Subject: [PATCH] UPBGE: Fix KX_Mesh.transformUV with invalid UV layer. This function can copy an uv layer to an other, but no check was proceeded for the source layer and created invalid memory access. To solve this issue and simplify the error checking for uv layers, the function uvExist is created as adviced by youle. This function check if the layer passed is between 0 and format.uvSize. Fix issue: #955. --- source/gameengine/Ketsji/KX_Mesh.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/source/gameengine/Ketsji/KX_Mesh.cpp b/source/gameengine/Ketsji/KX_Mesh.cpp index e930ea9623c7..bbfff89c1ffb 100644 --- a/source/gameengine/Ketsji/KX_Mesh.cpp +++ b/source/gameengine/Ketsji/KX_Mesh.cpp @@ -275,6 +275,11 @@ PyObject *KX_Mesh::PyTransform(PyObject *args, PyObject *kwds) Py_RETURN_NONE; } +static bool uvExist(short index, const RAS_DisplayArray::Format& format) +{ + return (0 <= index && index < format.uvSize); +} + PyObject *KX_Mesh::PyTransformUV(PyObject *args, PyObject *kwds) { int matindex; @@ -321,20 +326,23 @@ PyObject *KX_Mesh::PyTransformUV(PyObject *args, PyObject *kwds) ok = true; for (unsigned int j = 0, size = array->GetVertexCount(); j < size; ++j) { - if (uvindex_from != -1) { + // Copy one layer (optional). + if (uvindex_from != -1 && uvExist(uvindex_from, format)) { array->SetUv(j, uvindex, array->GetUv(j, uvindex_from)); } - if (0 <= uvindex && uvindex < format.uvSize) { - const mt::vec2_packed& uv = array->GetUv(j, uvindex); - array->SetUv(j, uvindex, (transform * mt::vec3(uv.x, uv.y, 0.0f)).xy()); - } - else if (uvindex == -1) { + // Transform all layers. + if (uvindex == -1) { for (unsigned short k = 0; k < format.uvSize; ++k) { const mt::vec2_packed& uv = array->GetUv(j, k); array->SetUv(j, k, (transform * mt::vec3(uv.x, uv.y, 0.0f)).xy()); } } + // Transform one layer. + else if (uvExist(uvindex, format)) { + const mt::vec2_packed& uv = array->GetUv(j, uvindex); + array->SetUv(j, uvindex, (transform * mt::vec3(uv.x, uv.y, 0.0f)).xy()); + } } array->NotifyUpdate(RAS_DisplayArray::UVS_MODIFIED);