From b6787b00144a3296c8cdbe623824d0a6b572e497 Mon Sep 17 00:00:00 2001 From: Alexei Kasatkin Date: Thu, 5 Jun 2014 23:14:36 +0600 Subject: [PATCH] safeguard: do not read/write 0 bytes (iostream) --- DataStructures/StaticRTree.h | 5 ++- Server/DataStructures/InternalDataFacade.h | 10 ++++-- Tools/components.cpp | 7 ++-- Util/GraphLoader.h | 5 ++- datastore.cpp | 37 ++++++++++++++++------ prepare.cpp | 15 +++++++-- 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/DataStructures/StaticRTree.h b/DataStructures/StaticRTree.h index 3b5b58f4fb2..33e4c09a703 100644 --- a/DataStructures/StaticRTree.h +++ b/DataStructures/StaticRTree.h @@ -450,7 +450,10 @@ class StaticRTree tree_node_file.read((char *)&tree_size, sizeof(uint32_t)); m_search_tree.resize(tree_size); - tree_node_file.read((char *)&m_search_tree[0], sizeof(TreeNode) * tree_size); + if (tree_size > 0) + { + tree_node_file.read((char *)&m_search_tree[0], sizeof(TreeNode) * tree_size); + } tree_node_file.close(); // open leaf node file and store thread specific pointer if (!boost::filesystem::exists(leaf_file)) diff --git a/Server/DataStructures/InternalDataFacade.h b/Server/DataStructures/InternalDataFacade.h index 20999af0b7e..4ca0862d431 100644 --- a/Server/DataStructures/InternalDataFacade.h +++ b/Server/DataStructures/InternalDataFacade.h @@ -172,16 +172,22 @@ template class InternalDataFacade : public BaseDataFacade 0) + { + geometry_stream.read((char *)&(m_geometry_indices[0]), number_of_indices * sizeof(unsigned)); + } geometry_stream.read((char *)&number_of_compressed_geometries, sizeof(unsigned)); BOOST_ASSERT(m_geometry_indices.back() == number_of_compressed_geometries); m_geometry_list.resize(number_of_compressed_geometries); - geometry_stream.read((char *)&(m_geometry_list[0]), + if (number_of_compressed_geometries > 0) + { + geometry_stream.read((char *)&(m_geometry_list[0]), number_of_compressed_geometries * sizeof(unsigned)); + } geometry_stream.close(); } diff --git a/Tools/components.cpp b/Tools/components.cpp index e0ba45a7541..945d94fa648 100644 --- a/Tools/components.cpp +++ b/Tools/components.cpp @@ -79,8 +79,11 @@ int main(int argc, char *argv[]) restriction_ifstream.read((char *)&usable_restriction_count, sizeof(uint32_t)); restrictions_vector.resize(usable_restriction_count); - restriction_ifstream.read((char *)&(restrictions_vector[0]), - usable_restriction_count * sizeof(TurnRestriction)); + if (usable_restriction_count>0) + { + restriction_ifstream.read((char *)&(restrictions_vector[0]), + usable_restriction_count * sizeof(TurnRestriction)); + } restriction_ifstream.close(); std::ifstream input_stream; diff --git a/Util/GraphLoader.h b/Util/GraphLoader.h index 5ec961ab84f..612332b6147 100644 --- a/Util/GraphLoader.h +++ b/Util/GraphLoader.h @@ -308,7 +308,10 @@ unsigned readHSGRFromStream(const boost::filesystem::path &hsgr_file, hsgr_input_stream.read((char *)&(node_list[0]), number_of_nodes * sizeof(NodeT)); edge_list.resize(number_of_edges); - hsgr_input_stream.read((char *)&(edge_list[0]), number_of_edges * sizeof(EdgeT)); + if (number_of_edges > 0) + { + hsgr_input_stream.read((char *)&(edge_list[0]), number_of_edges * sizeof(EdgeT)); + } hsgr_input_stream.close(); return number_of_nodes; diff --git a/datastore.cpp b/datastore.cpp index a4094ce74b4..ffb88462e28 100644 --- a/datastore.cpp +++ b/datastore.cpp @@ -332,12 +332,17 @@ int main(const int argc, const char *argv[]) // Loading street names unsigned *name_index_ptr = (unsigned *)(shared_memory_ptr + shared_layout_ptr->GetNameIndexOffset()); - - name_stream.read((char *)name_index_ptr, + if (shared_layout_ptr->name_index_list_size > 0) + { + name_stream.read((char *)name_index_ptr, shared_layout_ptr->name_index_list_size * sizeof(unsigned)); + } char *name_char_ptr = shared_memory_ptr + shared_layout_ptr->GetNameListOffset(); - name_stream.read(name_char_ptr, shared_layout_ptr->name_char_list_size * sizeof(char)); + if (shared_layout_ptr->name_char_list_size > 0) + { + name_stream.read(name_char_ptr, shared_layout_ptr->name_char_list_size * sizeof(char)); + } name_stream.close(); // load original edge information @@ -387,10 +392,12 @@ int main(const int argc, const char *argv[]) geometry_input_stream.seekg(0, geometry_input_stream.beg); geometry_input_stream.read((char *)&temporary_value, sizeof(unsigned)); BOOST_ASSERT(temporary_value == shared_layout_ptr->geometries_index_list_size); - - geometry_input_stream.read((char *)geometries_index_ptr, + if (shared_layout_ptr->geometries_index_list_size > 0) + { + geometry_input_stream.read((char *)geometries_index_ptr, shared_layout_ptr->geometries_index_list_size * sizeof(unsigned)); + } unsigned *geometries_list_ptr = (unsigned *)(shared_memory_ptr + shared_layout_ptr->GetGeometryListOffset()); @@ -398,8 +405,11 @@ int main(const int argc, const char *argv[]) geometry_input_stream.read((char *)&temporary_value, sizeof(unsigned)); BOOST_ASSERT(temporary_value == shared_layout_ptr->geometries_list_size); - geometry_input_stream.read((char *)geometries_list_ptr, + if (shared_layout_ptr->geometries_list_size > 0) + { + geometry_input_stream.read((char *)geometries_list_ptr, shared_layout_ptr->geometries_list_size * sizeof(unsigned)); + } // Loading list of coordinates FixedPointCoordinate *coordinates_ptr = @@ -423,24 +433,33 @@ int main(const int argc, const char *argv[]) char *rtree_ptr = static_cast(shared_memory_ptr + shared_layout_ptr->GetRSearchTreeOffset()); - tree_node_file.read(rtree_ptr, sizeof(RTreeNode) * tree_size); + if (tree_size > 0) + { + tree_node_file.read(rtree_ptr, sizeof(RTreeNode) * tree_size); + } tree_node_file.close(); // load the nodes of the search graph QueryGraph::NodeArrayEntry *graph_node_list_ptr = (QueryGraph::NodeArrayEntry *)(shared_memory_ptr + shared_layout_ptr->GetGraphNodeListOffset()); - hsgr_input_stream.read((char *)graph_node_list_ptr, + if (shared_layout_ptr->graph_node_list_size > 0) + { + hsgr_input_stream.read((char *)graph_node_list_ptr, shared_layout_ptr->graph_node_list_size * sizeof(QueryGraph::NodeArrayEntry)); + } // load the edges of the search graph QueryGraph::EdgeArrayEntry *graph_edge_list_ptr = (QueryGraph::EdgeArrayEntry *)(shared_memory_ptr + shared_layout_ptr->GetGraphEdgeListOffset()); - hsgr_input_stream.read((char *)graph_edge_list_ptr, + if (shared_layout_ptr->graph_edge_list_size > 0) + { + hsgr_input_stream.read((char *)graph_edge_list_ptr, shared_layout_ptr->graph_edge_list_size * sizeof(QueryGraph::EdgeArrayEntry)); + } hsgr_input_stream.close(); // acquire lock diff --git a/prepare.cpp b/prepare.cpp index 4fab6c5da46..f06286bcbea 100644 --- a/prepare.cpp +++ b/prepare.cpp @@ -203,8 +203,11 @@ int main(int argc, char *argv[]) restriction_stream.read((char *)&number_of_usable_restrictions, sizeof(unsigned)); restriction_list.resize(number_of_usable_restrictions); - restriction_stream.read((char *)&(restriction_list[0]), + if (number_of_usable_restrictions > 0) + { + restriction_stream.read((char *)&(restriction_list[0]), number_of_usable_restrictions * sizeof(TurnRestriction)); + } restriction_stream.close(); boost::filesystem::ifstream in; @@ -347,8 +350,11 @@ int main(int argc, char *argv[]) boost::filesystem::ofstream node_stream(node_filename, std::ios::binary); const unsigned size_of_mapping = internal_to_external_node_map.size(); node_stream.write((char *)&size_of_mapping, sizeof(unsigned)); - node_stream.write((char *)&(internal_to_external_node_map[0]), + if (size_of_mapping > 0) + { + node_stream.write((char *)&(internal_to_external_node_map[0]), size_of_mapping * sizeof(NodeInfo)); + } node_stream.close(); internal_to_external_node_map.clear(); internal_to_external_node_map.shrink_to_fit(); @@ -429,8 +435,11 @@ int main(int argc, char *argv[]) // serialize number of edges hsgr_output_stream.write((char *)&contracted_edge_count, sizeof(unsigned)); // serialize all nodes - hsgr_output_stream.write((char *)&node_array[0], + if (node_array_size > 0) + { + hsgr_output_stream.write((char *)&node_array[0], sizeof(StaticGraph::NodeArrayEntry) * node_array_size); + } // serialize all edges SimpleLogger().Write() << "Building edge array";