Skip to content

Commit d1dd264

Browse files
authored
Merge pull request #109668 from aaronfranke/4.3-import-mesh-validate-indices-mult-3
[4.3] ImporterMesh: Validate triangle indices array size is a multiple of 3
2 parents b8d8e1e + 79aefd4 commit d1dd264

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

modules/gltf/gltf_document.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,13 +2833,17 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
28332833
Vector<int> indices_vec4_mapping;
28342834
if (p.has("indices")) {
28352835
indices = _decode_accessor_as_ints(p_state, p["indices"], false);
2836-
const int is = indices.size();
2836+
const int index_count = indices.size();
28372837

28382838
if (primitive == Mesh::PRIMITIVE_TRIANGLES) {
2839+
if (index_count % 3 != 0) {
2840+
ERR_PRINT("glTF import: Mesh " + itos(i) + " surface " + itos(j) + " in file " + p_state->filename + " is invalid. Indexed triangle meshes MUST have an index array with a size that is a multiple of 3, but got " + itos(index_count) + " indices.");
2841+
continue;
2842+
}
28392843
// Swap around indices, convert ccw to cw for front face.
28402844

28412845
int *w = indices.ptrw();
2842-
for (int k = 0; k < is; k += 3) {
2846+
for (int k = 0; k < index_count; k += 3) {
28432847
SWAP(w[k + 1], w[k + 2]);
28442848
}
28452849
}
@@ -2848,7 +2852,7 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
28482852
Vector<bool> used_indices;
28492853
used_indices.resize_zeroed(orig_vertex_num);
28502854
bool *used_w = used_indices.ptrw();
2851-
for (int idx_i = 0; idx_i < is; idx_i++) {
2855+
for (int idx_i = 0; idx_i < index_count; idx_i++) {
28522856
ERR_FAIL_INDEX_V(indices_w[idx_i], orig_vertex_num, ERR_INVALID_DATA);
28532857
used_w[indices_w[idx_i]] = true;
28542858
}
@@ -3048,11 +3052,15 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
30483052
// Generate indices because they need to be swapped for CW/CCW.
30493053
const Vector<Vector3> &vertices = array[Mesh::ARRAY_VERTEX];
30503054
ERR_FAIL_COND_V(vertices.is_empty(), ERR_PARSE_ERROR);
3051-
const int vs = vertices.size();
3052-
indices.resize(vs);
3055+
const int vertex_count = vertices.size();
3056+
if (vertex_count % 3 != 0) {
3057+
ERR_PRINT("glTF import: Mesh " + itos(i) + " surface " + itos(j) + " in file " + p_state->filename + " is invalid. Non-indexed triangle meshes MUST have a vertex array with a size that is a multiple of 3, but got " + itos(vertex_count) + " vertices.");
3058+
continue;
3059+
}
3060+
indices.resize(vertex_count);
30533061
{
30543062
int *w = indices.ptrw();
3055-
for (int k = 0; k < vs; k += 3) {
3063+
for (int k = 0; k < vertex_count; k += 3) {
30563064
w[k] = k;
30573065
w[k + 1] = k + 2;
30583066
w[k + 2] = k + 1;

scene/resources/3d/importer_mesh.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
307307
if (index_count == 0) {
308308
continue; //no lods if no indices
309309
}
310+
ERR_FAIL_COND_MSG(index_count % 3 != 0, "ImporterMesh::generate_lods: Indexed triangle meshes MUST have an index array with a size that is a multiple of 3, but got " + itos(index_count) + " indices. Cannot generate LODs for this invalid mesh.");
310311

311312
const Vector3 *vertices_ptr = vertices.ptr();
312313
const int *indices_ptr = indices.ptr();

0 commit comments

Comments
 (0)