From ae9f5b9beea37327b42d949897751dc2c70a1fe4 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 20 Mar 2022 02:29:14 +0100 Subject: [PATCH] Fixed the remaining potential crashes found by the godot test tool. --- .../infos/spell_cast_info.cpp | 6 ++++- modules/props/prop_instance_merger.cpp | 3 ++- modules/props_2d/prop_2d_mesher.cpp | 12 ++++++++++ modules/terraman/meshers/terrain_mesher.cpp | 16 +++++++++++++ .../world/default/terrain_chunk_default.cpp | 3 ++- modules/terraman/world/jobs/terrain_job.cpp | 2 ++ .../terraman/world/jobs/terrain_prop_job.cpp | 2 ++ .../world/jobs/terrain_terrain_job.cpp | 2 ++ .../terraman_2d/meshers/terrain_2d_mesher.cpp | 12 ++++++++++ .../terraman_2d/world/jobs/terrain_2d_job.cpp | 2 ++ .../world/jobs/terrain_2d_prop_job.cpp | 2 ++ .../world/jobs/terrain_2d_terrain_job.cpp | 2 ++ .../meshers/cubic/voxel_cube_points.cpp | 2 ++ .../voxel_mesher_marching_cubes.cpp | 24 ++++++++++++++++++- modules/voxelman/meshers/voxel_mesher.cpp | 16 +++++++++++++ .../world/default/voxel_chunk_default.cpp | 3 ++- modules/voxelman/world/jobs/voxel_job.cpp | 2 ++ .../voxelman/world/jobs/voxel_prop_job.cpp | 2 ++ .../voxelman/world/jobs/voxel_terrain_job.cpp | 2 ++ 19 files changed, 110 insertions(+), 5 deletions(-) diff --git a/modules/entity_spell_system/infos/spell_cast_info.cpp b/modules/entity_spell_system/infos/spell_cast_info.cpp index abda7cd28..fc1e5eef9 100644 --- a/modules/entity_spell_system/infos/spell_cast_info.cpp +++ b/modules/entity_spell_system/infos/spell_cast_info.cpp @@ -196,7 +196,11 @@ Dictionary SpellCastInfo::to_dict() { dict["num_pushbacks"] = _num_pushbacks; dict["is_casting"] = _is_casting; - dict["spell_id"] = _spell->get_id(); + if (_spell.is_valid()) { + dict["spell_id"] = _spell->get_id(); + } else { + dict["spell_id"] = _spell_id; + } //item serialization not needed diff --git a/modules/props/prop_instance_merger.cpp b/modules/props/prop_instance_merger.cpp index 8abb9f684..89363c35e 100644 --- a/modules/props/prop_instance_merger.cpp +++ b/modules/props/prop_instance_merger.cpp @@ -371,8 +371,9 @@ void PropInstanceMerger::debug_mesh_allocate() { if (_debug_mesh_instance == RID()) { _debug_mesh_instance = VisualServer::get_singleton()->instance_create(); - if (get_world().is_valid()) + if (get_world().is_valid()) { VS::get_singleton()->instance_set_scenario(_debug_mesh_instance, get_world()->get_scenario()); + } VS::get_singleton()->instance_set_base(_debug_mesh_instance, _debug_mesh_rid); VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform()); diff --git a/modules/props_2d/prop_2d_mesher.cpp b/modules/props_2d/prop_2d_mesher.cpp index 7ef149af2..2cb394fde 100644 --- a/modules/props_2d/prop_2d_mesher.cpp +++ b/modules/props_2d/prop_2d_mesher.cpp @@ -991,10 +991,14 @@ void Prop2DMesher::add_vertex(const Vector2 &vertex) { } Vector2 Prop2DMesher::get_vertex(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).vertex; } void Prop2DMesher::remove_vertex(const int idx) { + ERR_FAIL_INDEX(idx, _vertices.size()); + _vertices.remove(idx); } @@ -1026,6 +1030,8 @@ void Prop2DMesher::add_color(const Color &color) { } Color Prop2DMesher::get_color(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Color()); + return _vertices.get(idx).color; } @@ -1057,6 +1063,8 @@ void Prop2DMesher::add_uv(const Vector2 &uv) { } Vector2 Prop2DMesher::get_uv(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).uv; } @@ -1077,10 +1085,14 @@ void Prop2DMesher::add_indices(const int index) { } int Prop2DMesher::get_index(const int idx) const { + ERR_FAIL_INDEX_V(idx, _indices.size(), 0); + return _indices.get(idx); } void Prop2DMesher::remove_index(const int idx) { + ERR_FAIL_INDEX(idx, _indices.size()); + _indices.remove(idx); } diff --git a/modules/terraman/meshers/terrain_mesher.cpp b/modules/terraman/meshers/terrain_mesher.cpp index c043255f8..f48c26c71 100644 --- a/modules/terraman/meshers/terrain_mesher.cpp +++ b/modules/terraman/meshers/terrain_mesher.cpp @@ -726,10 +726,14 @@ void TerrainMesher::add_vertex(const Vector3 &vertex) { } Vector3 TerrainMesher::get_vertex(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector3()); + return _vertices.get(idx).vertex; } void TerrainMesher::remove_vertex(const int idx) { + ERR_FAIL_INDEX(idx, _vertices.size()); + _vertices.remove(idx); } @@ -761,6 +765,8 @@ void TerrainMesher::add_normal(const Vector3 &normal) { } Vector3 TerrainMesher::get_normal(int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector3()); + return _vertices.get(idx).normal; } @@ -792,6 +798,8 @@ void TerrainMesher::add_color(const Color &color) { } Color TerrainMesher::get_color(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Color()); + return _vertices.get(idx).color; } @@ -823,6 +831,8 @@ void TerrainMesher::add_uv(const Vector2 &uv) { } Vector2 TerrainMesher::get_uv(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).uv; } @@ -854,6 +864,8 @@ void TerrainMesher::add_uv2(const Vector2 &uv) { } Vector2 TerrainMesher::get_uv2(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).uv2; } @@ -874,10 +886,14 @@ void TerrainMesher::add_indices(const int index) { } int TerrainMesher::get_index(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), 0); + return _indices.get(idx); } void TerrainMesher::remove_index(const int idx) { + ERR_FAIL_INDEX(idx, _vertices.size()); + _indices.remove(idx); } diff --git a/modules/terraman/world/default/terrain_chunk_default.cpp b/modules/terraman/world/default/terrain_chunk_default.cpp index 4c9c62f81..5791782c7 100644 --- a/modules/terraman/world/default/terrain_chunk_default.cpp +++ b/modules/terraman/world/default/terrain_chunk_default.cpp @@ -533,8 +533,9 @@ void TerrainChunkDefault::debug_mesh_allocate() { if (_debug_mesh_instance == RID()) { _debug_mesh_instance = VisualServer::get_singleton()->instance_create(); - if (get_voxel_world()->get_world().is_valid()) + if (get_voxel_world() && get_voxel_world()->get_world().is_valid()) { VS::get_singleton()->instance_set_scenario(_debug_mesh_instance, get_voxel_world()->get_world()->get_scenario()); + } VS::get_singleton()->instance_set_base(_debug_mesh_instance, _debug_mesh_rid); VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform()); diff --git a/modules/terraman/world/jobs/terrain_job.cpp b/modules/terraman/world/jobs/terrain_job.cpp index 15faf861f..5e2780958 100644 --- a/modules/terraman/world/jobs/terrain_job.cpp +++ b/modules/terraman/world/jobs/terrain_job.cpp @@ -59,6 +59,8 @@ void TerrainJob::set_build_done(const bool val) { } void TerrainJob::next_job() { + ERR_FAIL_COND(!_chunk.is_valid()); + _chunk->job_next(); set_build_done(true); } diff --git a/modules/terraman/world/jobs/terrain_prop_job.cpp b/modules/terraman/world/jobs/terrain_prop_job.cpp index 8a73ecbb0..843eab1e1 100644 --- a/modules/terraman/world/jobs/terrain_prop_job.cpp +++ b/modules/terraman/world/jobs/terrain_prop_job.cpp @@ -74,6 +74,8 @@ int TerrainPropJob::get_jobs_step_count() const { } void TerrainPropJob::phase_physics_process() { + ERR_FAIL_COND(!_chunk.is_valid()); + Ref chunk = _chunk; //TODO this should only update the differences diff --git a/modules/terraman/world/jobs/terrain_terrain_job.cpp b/modules/terraman/world/jobs/terrain_terrain_job.cpp index 86460b76f..e27a59487 100644 --- a/modules/terraman/world/jobs/terrain_terrain_job.cpp +++ b/modules/terraman/world/jobs/terrain_terrain_job.cpp @@ -202,6 +202,8 @@ void TerrainTerrainJob::phase_collider() { } void TerrainTerrainJob::phase_physics_process() { + ERR_FAIL_COND(!_chunk.is_valid()); + Ref chunk = _chunk; if (temp_arr_collider.size() != 0) { diff --git a/modules/terraman_2d/meshers/terrain_2d_mesher.cpp b/modules/terraman_2d/meshers/terrain_2d_mesher.cpp index 530e7613b..f0555559b 100644 --- a/modules/terraman_2d/meshers/terrain_2d_mesher.cpp +++ b/modules/terraman_2d/meshers/terrain_2d_mesher.cpp @@ -748,10 +748,14 @@ void Terrain2DMesher::add_vertex(const Vector2 &vertex) { } Vector2 Terrain2DMesher::get_vertex(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).vertex; } void Terrain2DMesher::remove_vertex(const int idx) { + ERR_FAIL_INDEX(idx, _vertices.size()); + _vertices.remove(idx); } @@ -783,6 +787,8 @@ void Terrain2DMesher::add_color(const Color &color) { } Color Terrain2DMesher::get_color(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Color()); + return _vertices.get(idx).color; } @@ -814,6 +820,8 @@ void Terrain2DMesher::add_uv(const Vector2 &uv) { } Vector2 Terrain2DMesher::get_uv(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).uv; } @@ -834,10 +842,14 @@ void Terrain2DMesher::add_indices(const int index) { } int Terrain2DMesher::get_index(const int idx) const { + ERR_FAIL_INDEX_V(idx, _indices.size(), 0); + return _indices.get(idx); } void Terrain2DMesher::remove_index(const int idx) { + ERR_FAIL_INDEX(idx, _indices.size()); + _indices.remove(idx); } diff --git a/modules/terraman_2d/world/jobs/terrain_2d_job.cpp b/modules/terraman_2d/world/jobs/terrain_2d_job.cpp index 37b0739cd..ab82b0f2e 100644 --- a/modules/terraman_2d/world/jobs/terrain_2d_job.cpp +++ b/modules/terraman_2d/world/jobs/terrain_2d_job.cpp @@ -59,6 +59,8 @@ void Terrain2DJob::set_build_done(const bool val) { } void Terrain2DJob::next_job() { + ERR_FAIL_COND(!_chunk.is_valid()); + _chunk->job_next(); set_build_done(true); } diff --git a/modules/terraman_2d/world/jobs/terrain_2d_prop_job.cpp b/modules/terraman_2d/world/jobs/terrain_2d_prop_job.cpp index ae437ded4..6b602a999 100644 --- a/modules/terraman_2d/world/jobs/terrain_2d_prop_job.cpp +++ b/modules/terraman_2d/world/jobs/terrain_2d_prop_job.cpp @@ -50,6 +50,8 @@ void Terrain2DProp2DJob::set_prop_mesher(const Ref &mesher) { } void Terrain2DProp2DJob::phase_physics_process() { + ERR_FAIL_COND(!_chunk.is_valid()); + /* Ref chunk = _chunk; diff --git a/modules/terraman_2d/world/jobs/terrain_2d_terrain_job.cpp b/modules/terraman_2d/world/jobs/terrain_2d_terrain_job.cpp index 6940e0464..165c06f2e 100644 --- a/modules/terraman_2d/world/jobs/terrain_2d_terrain_job.cpp +++ b/modules/terraman_2d/world/jobs/terrain_2d_terrain_job.cpp @@ -199,6 +199,8 @@ void Terrain2DTerrain2DJob::phase_collider() { } void Terrain2DTerrain2DJob::phase_physics_process() { + ERR_FAIL_COND(!_chunk.is_valid()); + Ref chunk = _chunk; if (temp_arr_collider.size() != 0) { diff --git a/modules/voxelman/meshers/cubic/voxel_cube_points.cpp b/modules/voxelman/meshers/cubic/voxel_cube_points.cpp index 5582f08ce..497c3bbd8 100644 --- a/modules/voxelman/meshers/cubic/voxel_cube_points.cpp +++ b/modules/voxelman/meshers/cubic/voxel_cube_points.cpp @@ -568,6 +568,8 @@ bool VoxelCubePoints::is_sub_voxel_point(int x, int y, int z) { } void VoxelCubePoints::set_point(int point, int x, int y, int z) { + ERR_FAIL_INDEX(point, POINT_COUNT); + _points[point] = Vector3(x, y, z); } diff --git a/modules/voxelman/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp b/modules/voxelman/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp index 715b4aea9..eab8961d2 100644 --- a/modules/voxelman/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp +++ b/modules/voxelman/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp @@ -464,20 +464,27 @@ void VoxelMesherMarchingCubes::_add_chunk(Ref p_chunk) { } Vector3 VoxelMesherMarchingCubes::corner_id_to_vertex(int corner_id) const { - ERR_FAIL_COND_V(corner_id < 0 || corner_id > 8, Vector3()); + ERR_FAIL_INDEX_V(corner_id, 8, Vector3()); return marching_cube_vertices[corner_id]; } int VoxelMesherMarchingCubes::get_regular_cell_class(int index) const { + ERR_FAIL_INDEX_V(index, 256, 0); + return static_cast(regularCellClass[index]); } Ref VoxelMesherMarchingCubes::get_regular_cell_data(int index) const { + ERR_FAIL_INDEX_V(index, 16, 0); + return _regular_cell_datas[index]; } int VoxelMesherMarchingCubes::get_regular_vertex_data(int index1, int index2) const { + ERR_FAIL_INDEX_V(index1, 256, 0); + ERR_FAIL_INDEX_V(index2, 15, 0); + //return static_cast(regularVertexData[index1][index2]); return regularVertexData[index1][index2]; } @@ -490,30 +497,45 @@ int VoxelMesherMarchingCubes::get_regular_vertex_data(int index1, int index2) co //} int VoxelMesherMarchingCubes::get_regular_vertex_data_first_vertex(int index1, int index2) const { + ERR_FAIL_INDEX_V(index1, 256, 0); + ERR_FAIL_INDEX_V(index2, 15, 0); + int vert1 = regularVertexData[index1][index2] & 0x000F; return vert1; } int VoxelMesherMarchingCubes::get_regular_vertex_data_second_vertex(int index1, int index2) const { + ERR_FAIL_INDEX_V(index1, 256, 0); + ERR_FAIL_INDEX_V(index2, 15, 0); + int vert2 = (regularVertexData[index1][index2] & 0x00F0) >> 4; return vert2; } Vector3 VoxelMesherMarchingCubes::get_regular_vertex_first_position(int index1, int index2) const { + ERR_FAIL_INDEX_V(index1, 256, Vector3()); + ERR_FAIL_INDEX_V(index2, 15, Vector3()); + int vert = regularVertexData[index1][index2] & 0x000F; return marching_cube_vertices[vert]; } Vector3 VoxelMesherMarchingCubes::get_regular_vertex_second_position(int index1, int index2) const { + ERR_FAIL_INDEX_V(index1, 256, Vector3()); + ERR_FAIL_INDEX_V(index2, 15, Vector3()); + int vert = (regularVertexData[index1][index2] & 0x00F0) >> 4; return marching_cube_vertices[vert]; } Vector3 VoxelMesherMarchingCubes::get_regular_vertex_direction(int index1, int index2) const { + ERR_FAIL_INDEX_V(index1, 256, Vector3()); + ERR_FAIL_INDEX_V(index2, 15, Vector3()); + int vert1 = regularVertexData[index1][index2] & 0x000F; int vert2 = (regularVertexData[index1][index2] & 0x00F0) >> 4; diff --git a/modules/voxelman/meshers/voxel_mesher.cpp b/modules/voxelman/meshers/voxel_mesher.cpp index b3f61e35f..0196a9357 100644 --- a/modules/voxelman/meshers/voxel_mesher.cpp +++ b/modules/voxelman/meshers/voxel_mesher.cpp @@ -720,10 +720,14 @@ void VoxelMesher::add_vertex(const Vector3 &vertex) { } Vector3 VoxelMesher::get_vertex(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector3()); + return _vertices.get(idx).vertex; } void VoxelMesher::remove_vertex(const int idx) { + ERR_FAIL_INDEX(idx, _vertices.size()); + _vertices.remove(idx); } @@ -755,6 +759,8 @@ void VoxelMesher::add_normal(const Vector3 &normal) { } Vector3 VoxelMesher::get_normal(int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector3()); + return _vertices.get(idx).normal; } @@ -786,6 +792,8 @@ void VoxelMesher::add_color(const Color &color) { } Color VoxelMesher::get_color(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Color()); + return _vertices.get(idx).color; } @@ -817,6 +825,8 @@ void VoxelMesher::add_uv(const Vector2 &uv) { } Vector2 VoxelMesher::get_uv(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).uv; } @@ -848,6 +858,8 @@ void VoxelMesher::add_uv2(const Vector2 &uv) { } Vector2 VoxelMesher::get_uv2(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), Vector2()); + return _vertices.get(idx).uv2; } @@ -868,10 +880,14 @@ void VoxelMesher::add_indices(const int index) { } int VoxelMesher::get_index(const int idx) const { + ERR_FAIL_INDEX_V(idx, _vertices.size(), 0); + return _indices.get(idx); } void VoxelMesher::remove_index(const int idx) { + ERR_FAIL_INDEX(idx, _vertices.size()); + _indices.remove(idx); } diff --git a/modules/voxelman/world/default/voxel_chunk_default.cpp b/modules/voxelman/world/default/voxel_chunk_default.cpp index 2fe01eb7f..d83073940 100644 --- a/modules/voxelman/world/default/voxel_chunk_default.cpp +++ b/modules/voxelman/world/default/voxel_chunk_default.cpp @@ -531,8 +531,9 @@ void VoxelChunkDefault::debug_mesh_allocate() { if (_debug_mesh_instance == RID()) { _debug_mesh_instance = VisualServer::get_singleton()->instance_create(); - if (get_voxel_world()->get_world().is_valid()) + if (get_voxel_world() && get_voxel_world()->get_world().is_valid()) { VS::get_singleton()->instance_set_scenario(_debug_mesh_instance, get_voxel_world()->get_world()->get_scenario()); + } VS::get_singleton()->instance_set_base(_debug_mesh_instance, _debug_mesh_rid); VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform()); diff --git a/modules/voxelman/world/jobs/voxel_job.cpp b/modules/voxelman/world/jobs/voxel_job.cpp index e3a7a2bc2..52f3b3d42 100644 --- a/modules/voxelman/world/jobs/voxel_job.cpp +++ b/modules/voxelman/world/jobs/voxel_job.cpp @@ -59,6 +59,8 @@ void VoxelJob::set_build_done(const bool val) { } void VoxelJob::next_job() { + ERR_FAIL_COND(!_chunk.is_valid()); + _chunk->job_next(); set_build_done(true); } diff --git a/modules/voxelman/world/jobs/voxel_prop_job.cpp b/modules/voxelman/world/jobs/voxel_prop_job.cpp index a67ee8920..841b6b1f8 100644 --- a/modules/voxelman/world/jobs/voxel_prop_job.cpp +++ b/modules/voxelman/world/jobs/voxel_prop_job.cpp @@ -75,6 +75,8 @@ int VoxelPropJob::get_jobs_step_count() const { } void VoxelPropJob::phase_physics_process() { + ERR_FAIL_COND(!_chunk.is_valid()); + Ref chunk = _chunk; //TODO this should only update the differences diff --git a/modules/voxelman/world/jobs/voxel_terrain_job.cpp b/modules/voxelman/world/jobs/voxel_terrain_job.cpp index 4d626be87..eaaff61ca 100644 --- a/modules/voxelman/world/jobs/voxel_terrain_job.cpp +++ b/modules/voxelman/world/jobs/voxel_terrain_job.cpp @@ -282,6 +282,8 @@ void VoxelTerrainJob::phase_collider() { } void VoxelTerrainJob::phase_physics_process() { + ERR_FAIL_COND(!_chunk.is_valid()); + Ref chunk = _chunk; if (temp_arr_collider.size() != 0) {