Skip to content

Commit 98b09ee

Browse files
aaronfrankeAdriaandeJongh
authored andcommitted
ImporterMesh: Validate triangle indices array size is a multiple of 3
1 parent ebfe5a7 commit 98b09ee

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

editor/import/3d/resource_importer_scene.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,9 +2608,10 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
26082608
mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
26092609
mesh_node->merge_meta_from(src_mesh_node);
26102610

2611-
if (src_mesh_node->get_mesh().is_valid()) {
2611+
Ref<ImporterMesh> importer_mesh = src_mesh_node->get_mesh();
2612+
if (importer_mesh.is_valid()) {
26122613
Ref<ArrayMesh> mesh;
2613-
if (!src_mesh_node->get_mesh()->has_mesh()) {
2614+
if (!importer_mesh->has_mesh()) {
26142615
//do mesh processing
26152616

26162617
bool generate_lods = p_generate_lods;
@@ -2619,7 +2620,7 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
26192620
bool bake_lightmaps = p_light_bake_mode == LIGHT_BAKE_STATIC_LIGHTMAPS;
26202621
String save_to_file;
26212622

2622-
String mesh_id = src_mesh_node->get_mesh()->get_meta("import_id", src_mesh_node->get_mesh()->get_name());
2623+
String mesh_id = importer_mesh->get_meta("import_id", importer_mesh->get_name());
26232624

26242625
if (!mesh_id.is_empty() && p_mesh_data.has(mesh_id)) {
26252626
Dictionary mesh_settings = p_mesh_data[mesh_id];
@@ -2673,7 +2674,7 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
26732674
}
26742675

26752676
for (int i = 0; i < post_importer_plugins.size(); i++) {
2676-
post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MESH, nullptr, src_mesh_node, src_mesh_node->get_mesh(), mesh_settings);
2677+
post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MESH, nullptr, src_mesh_node, importer_mesh, mesh_settings);
26772678
}
26782679
}
26792680

@@ -2686,7 +2687,7 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
26862687
}
26872688

26882689
Vector<uint8_t> lightmap_cache;
2689-
src_mesh_node->get_mesh()->lightmap_unwrap_cached(xf, p_lightmap_texel_size, p_src_lightmap_cache, lightmap_cache);
2690+
importer_mesh->lightmap_unwrap_cached(xf, p_lightmap_texel_size, p_src_lightmap_cache, lightmap_cache);
26902691

26912692
if (!lightmap_cache.is_empty()) {
26922693
if (r_lightmap_caches.is_empty()) {
@@ -2711,14 +2712,14 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
27112712

27122713
if (generate_lods) {
27132714
Array skin_pose_transform_array = _get_skinned_pose_transforms(src_mesh_node);
2714-
src_mesh_node->get_mesh()->generate_lods(merge_angle, skin_pose_transform_array);
2715+
importer_mesh->generate_lods(merge_angle, skin_pose_transform_array);
27152716
}
27162717

27172718
if (create_shadow_meshes) {
2718-
src_mesh_node->get_mesh()->create_shadow_mesh();
2719+
importer_mesh->create_shadow_mesh();
27192720
}
27202721

2721-
src_mesh_node->get_mesh()->optimize_indices();
2722+
importer_mesh->optimize_indices();
27222723

27232724
if (!save_to_file.is_empty()) {
27242725
String save_res_path = ResourceUID::ensure_path(save_to_file);
@@ -2727,7 +2728,7 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
27272728
//if somehow an existing one is useful, create
27282729
existing->reset_state();
27292730
}
2730-
mesh = src_mesh_node->get_mesh()->get_mesh(existing);
2731+
mesh = importer_mesh->get_mesh(existing);
27312732

27322733
Error err = ResourceSaver::save(mesh, save_res_path); //override
27332734
if (err != OK) {
@@ -2741,19 +2742,19 @@ Node *ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_
27412742
mesh->set_path(save_res_path, true); //takeover existing, if needed
27422743

27432744
} else {
2744-
mesh = src_mesh_node->get_mesh()->get_mesh();
2745+
mesh = importer_mesh->get_mesh();
27452746
}
27462747
} else {
2747-
mesh = src_mesh_node->get_mesh()->get_mesh();
2748+
mesh = importer_mesh->get_mesh();
27482749
}
27492750

27502751
if (mesh.is_valid()) {
2751-
_copy_meta(src_mesh_node->get_mesh().ptr(), mesh.ptr());
2752+
_copy_meta(importer_mesh.ptr(), mesh.ptr());
27522753
mesh_node->set_mesh(mesh);
27532754
for (int i = 0; i < mesh->get_surface_count(); i++) {
27542755
mesh_node->set_surface_override_material(i, src_mesh_node->get_surface_material(i));
27552756
}
2756-
mesh->merge_meta_from(*src_mesh_node->get_mesh());
2757+
mesh->merge_meta_from(*importer_mesh);
27572758
}
27582759
}
27592760

modules/gltf/gltf_document.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,13 +3340,14 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
33403340
Vector<int> indices_vec4_mapping;
33413341
if (mesh_prim.has("indices")) {
33423342
indices = _decode_accessor_as_ints(p_state, mesh_prim["indices"], false);
3343-
const int is = indices.size();
3343+
const int index_count = indices.size();
33443344

33453345
if (primitive == Mesh::PRIMITIVE_TRIANGLES) {
3346+
ERR_FAIL_COND_V_MSG(index_count % 3 != 0, ERR_PARSE_ERROR, "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.");
33463347
// Swap around indices, convert ccw to cw for front face.
33473348

33483349
int *w = indices.ptrw();
3349-
for (int k = 0; k < is; k += 3) {
3350+
for (int k = 0; k < index_count; k += 3) {
33503351
SWAP(w[k + 1], w[k + 2]);
33513352
}
33523353
}
@@ -3355,7 +3356,7 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
33553356
Vector<bool> used_indices;
33563357
used_indices.resize_initialized(orig_vertex_num);
33573358
bool *used_w = used_indices.ptrw();
3358-
for (int idx_i = 0; idx_i < is; idx_i++) {
3359+
for (int idx_i = 0; idx_i < index_count; idx_i++) {
33593360
ERR_FAIL_INDEX_V(indices_w[idx_i], orig_vertex_num, ERR_INVALID_DATA);
33603361
used_w[indices_w[idx_i]] = true;
33613362
}
@@ -3556,11 +3557,12 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
35563557
// Generate indices because they need to be swapped for CW/CCW.
35573558
const Vector<Vector3> &vertices = array[Mesh::ARRAY_VERTEX];
35583559
ERR_FAIL_COND_V(vertices.is_empty(), ERR_PARSE_ERROR);
3559-
const int vs = vertices.size();
3560-
indices.resize(vs);
3560+
const int vertex_count = vertices.size();
3561+
ERR_FAIL_COND_V_MSG(vertex_count % 3 != 0, ERR_PARSE_ERROR, "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.");
3562+
indices.resize(vertex_count);
35613563
{
35623564
int *w = indices.ptrw();
3563-
for (int k = 0; k < vs; k += 3) {
3565+
for (int k = 0; k < vertex_count; k += 3) {
35643566
w[k] = k;
35653567
w[k + 1] = k + 2;
35663568
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
@@ -322,6 +322,7 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, Array p_bone_transf
322322
if (index_count == 0) {
323323
continue; //no lods if no indices
324324
}
325+
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.");
325326

326327
const Vector3 *vertices_ptr = vertices.ptr();
327328
const int *indices_ptr = indices.ptr();

0 commit comments

Comments
 (0)