Browse Source

Fixes crash when using Mesh::create_outline and Mesh::create_convex_shape

Adds a size check to the array returned by `surface_get_arrays`.

During debugging, `create_outline` also crashes when the indices size is
one (not a multiple of three). For now, just reports the error and fail
the function.
Haoyu Qiu 5 years ago
parent
commit
3df9d187a3
1 changed files with 4 additions and 0 deletions
  1. 4 0
      scene/resources/mesh.cpp

+ 4 - 0
scene/resources/mesh.cpp

@@ -75,6 +75,7 @@ Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
 			continue;
 			continue;
 
 
 		Array a = surface_get_arrays(i);
 		Array a = surface_get_arrays(i);
+		ERR_FAIL_COND_V(a.empty(), Ref<TriangleMesh>());
 
 
 		int vc = surface_get_array_len(i);
 		int vc = surface_get_array_len(i);
 		PoolVector<Vector3> vertices = a[ARRAY_VERTEX];
 		PoolVector<Vector3> vertices = a[ARRAY_VERTEX];
@@ -234,6 +235,7 @@ Ref<Shape> Mesh::create_convex_shape() const {
 	for (int i = 0; i < get_surface_count(); i++) {
 	for (int i = 0; i < get_surface_count(); i++) {
 
 
 		Array a = surface_get_arrays(i);
 		Array a = surface_get_arrays(i);
+		ERR_FAIL_COND_V(a.empty(), Ref<ConvexPolygonShape>());
 		PoolVector<Vector3> v = a[ARRAY_VERTEX];
 		PoolVector<Vector3> v = a[ARRAY_VERTEX];
 		vertices.append_array(v);
 		vertices.append_array(v);
 	}
 	}
@@ -273,6 +275,7 @@ Ref<Mesh> Mesh::create_outline(float p_margin) const {
 			continue;
 			continue;
 
 
 		Array a = surface_get_arrays(i);
 		Array a = surface_get_arrays(i);
+		ERR_FAIL_COND_V(a.empty(), Ref<ArrayMesh>());
 
 
 		if (i == 0) {
 		if (i == 0) {
 			arrays = a;
 			arrays = a;
@@ -378,6 +381,7 @@ Ref<Mesh> Mesh::create_outline(float p_margin) const {
 		PoolVector<Vector3>::Write r = vertices.write();
 		PoolVector<Vector3>::Write r = vertices.write();
 
 
 		if (indices.size()) {
 		if (indices.size()) {
+			ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>());
 			vc = indices.size();
 			vc = indices.size();
 			ir = indices.write();
 			ir = indices.write();
 			has_indices = true;
 			has_indices = true;