From 78f13c01362d024522df0a4f68a8e99c462026a4 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Mon, 2 Oct 2023 12:01:15 -0700 Subject: [PATCH] Avoid LOD generation crash. --- scene/resources/importer_mesh.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scene/resources/importer_mesh.cpp b/scene/resources/importer_mesh.cpp index 092ba84aa5d7..69650626fb91 100644 --- a/scene/resources/importer_mesh.cpp +++ b/scene/resources/importer_mesh.cpp @@ -507,7 +507,9 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli int32_t *new_indices_ptr = new_indices.ptrw(); - int current_ray_count = 0; + int64_t current_ray_count = 0; + const int64_t MAX_MESH_INDICES = pow(2, 24); + ERR_BREAK(new_index_count >= MAX_MESH_INDICES); for (unsigned int j = 0; j < new_index_count; j += 3) { const Vector3 &v0 = vertices_ptr[new_indices_ptr[j + 0]]; const Vector3 &v1 = vertices_ptr[new_indices_ptr[j + 1]]; @@ -522,9 +524,11 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli Vector3 dir = face_normal / face_area; int ray_count = CLAMP(5.0 * face_area * error_factor, 16, 64); + ERR_BREAK(current_ray_count + ray_count >= MAX_MESH_INDICES); rays.resize(current_ray_count + ray_count); StaticRaycaster::Ray *rays_ptr = rays.ptrw(); + ERR_BREAK(current_ray_count + ray_count >= MAX_MESH_INDICES); ray_uvs.resize(current_ray_count + ray_count); Vector2 *ray_uvs_ptr = ray_uvs.ptr(); @@ -543,12 +547,14 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli Vector3 org = v0 * w + v1 * u + v2 * v; org -= dir * ray_bias; + ERR_BREAK(current_ray_count + k >= MAX_MESH_INDICES); rays_ptr[current_ray_count + k] = StaticRaycaster::Ray(org, dir, 0.0f, ray_length); rays_ptr[current_ray_count + k].id = j / 3; ray_uvs_ptr[current_ray_count + k] = Vector2(u, v); } current_ray_count += ray_count; + ERR_BREAK(current_ray_count >= MAX_MESH_INDICES); } raycaster->intersect(rays); @@ -556,6 +562,7 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli LocalVector ray_normals; LocalVector ray_normal_weights; + ERR_BREAK(new_index_count >= MAX_MESH_INDICES); ray_normals.resize(new_index_count); ray_normal_weights.resize(new_index_count);