From a99dc7ee578c95a44b6c3ae42c7e1af8ce20aa50 Mon Sep 17 00:00:00 2001 From: Timo Heister Date: Mon, 21 Mar 2016 14:18:58 +0100 Subject: [PATCH] add checks to GridTools::delete_*_vertices Check the inputs to GridTools::delete_unused_vertices() and GridTools::delete_duplicated_vertices() --- source/grid/grid_tools.cc | 86 ++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 20 deletions(-) diff --git a/source/grid/grid_tools.cc b/source/grid/grid_tools.cc index 61653fd841..3c4fc92b2b 100644 --- a/source/grid/grid_tools.cc +++ b/source/grid/grid_tools.cc @@ -367,16 +367,31 @@ namespace GridTools std::vector > &cells, SubCellData &subcelldata) { - // first check which vertices are - // actually used + Assert(subcelldata.check_consistency(dim), + ExcMessage("Invalid SubCellData supplied according to ::check_consistency(). " + "This is caused by data containing objects for the wrong dimension.")); + + // first check which vertices are actually used std::vector vertex_used (vertices.size(), false); for (unsigned int c=0; c::vertices_per_cell; ++v) - vertex_used[cells[c].vertices[v]] = true; + { + Assert(cells[c].vertices[v] < vertices.size(), + ExcMessage("Invalid vertex index encountered! cells[" + + Utilities::int_to_string(c) + + "].vertices[" + + Utilities::int_to_string(v) + + "]=" + + Utilities::int_to_string(cells[c].vertices[v]) + + " is invalid, because only " + + Utilities::int_to_string(vertices.size()) + + " vertices were supplied.")); + vertex_used[cells[c].vertices[v]] = true; + } + - // then renumber the vertices that - // are actually used in the same - // order as they were beforehand + // then renumber the vertices that are actually used in the same order as + // they were beforehand const unsigned int invalid_vertex = numbers::invalid_unsigned_int; std::vector new_vertex_numbers (vertices.size(), invalid_vertex); unsigned int next_free_number = 0; @@ -385,10 +400,9 @@ namespace GridTools { new_vertex_numbers[i] = next_free_number; ++next_free_number; - }; + } - // next replace old vertex numbers - // by the new ones + // next replace old vertex numbers by the new ones for (unsigned int c=0; c::vertices_per_cell; ++v) cells[c].vertices[v] = new_vertex_numbers[cells[c].vertices[v]]; @@ -396,17 +410,43 @@ namespace GridTools // same for boundary data for (unsigned int c=0; c::vertices_per_cell; ++v) - subcelldata.boundary_lines[c].vertices[v] - = new_vertex_numbers[subcelldata.boundary_lines[c].vertices[v]]; + { + Assert(subcelldata.boundary_lines[c].vertices[v] < new_vertex_numbers.size(), + ExcMessage("Invalid vertex index in subcelldata.boundary_lines. " + "subcelldata.boundary_lines[" + + Utilities::int_to_string(c) + + "].vertices[" + + Utilities::int_to_string(v) + + "]=" + + Utilities::int_to_string(subcelldata.boundary_lines[c].vertices[v]) + + " is invalid, because only " + + Utilities::int_to_string(vertices.size()) + + " vertices were supplied.")); + subcelldata.boundary_lines[c].vertices[v] + = new_vertex_numbers[subcelldata.boundary_lines[c].vertices[v]]; + } + for (unsigned int c=0; c::vertices_per_cell; ++v) - subcelldata.boundary_quads[c].vertices[v] - = new_vertex_numbers[subcelldata.boundary_quads[c].vertices[v]]; + { + Assert(subcelldata.boundary_quads[c].vertices[v] < new_vertex_numbers.size(), + ExcMessage("Invalid vertex index in subcelldata.boundary_quads. " + "subcelldata.boundary_quads[" + + Utilities::int_to_string(c) + + "].vertices[" + + Utilities::int_to_string(v) + + "]=" + + Utilities::int_to_string(subcelldata.boundary_quads[c].vertices[v]) + + " is invalid, because only " + + Utilities::int_to_string(vertices.size()) + + " vertices were supplied.")); + + subcelldata.boundary_quads[c].vertices[v] + = new_vertex_numbers[subcelldata.boundary_quads[c].vertices[v]]; + } - // finally copy over the vertices - // which we really need to a new - // array and replace the old one by - // the new one + // finally copy over the vertices which we really need to a new array and + // replace the old one by the new one std::vector > tmp; tmp.reserve (std::count(vertex_used.begin(), vertex_used.end(), true)); for (unsigned int v=0; v new_vertex_numbers(vertices.size()); for (unsigned int i=0; i::vertices_per_cell; ++v) cells[c].vertices[v]=new_vertex_numbers[cells[c].vertices[v]]; - delete_unused_vertices(vertices,cells,subcelldata); + delete_unused_vertices(vertices, cells, subcelldata); } -- 2.39.5