From a350040aa770b90a67cb30e3bdbf0551b57f9381 Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Fri, 17 Jan 2025 07:56:26 -0800 Subject: [PATCH] bugfix: string_view of invalid memory (#23417) ### Description the `std::unordered_map` uses a `std::string_view` as key, while the string view may refer to invalid memory. Function `IdentityBuilder` returns a `std::string` which goes out of scope quickly. ```c++ unordered_map> identical_children_map; for (auto i = node->OutputEdgesBegin(); i != node->OutputEdgesEnd(); ++i) { if (i->GetNode().OpType() == op) { identical_children_map[IdentityBuilder(graph, i->GetNode())].push_back(i->GetNode().Index()); } } ``` This code will cause a waring as error in EMSDK v4.0.1: ``` C:/code/o2/onnxruntime/core/optimizer/identical_children_consolidation.cc:51:30: error: object whose reference is captured by 'identical_children_map' will be destroyed at the end of the full-expression [-Werror,-Wdangling-capture] 51 | identical_children_map[IdentityBuilder(graph, i->GetNode())].push_back(i->GetNode().Index()); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. ``` --- .../core/optimizer/identical_children_consolidation.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/optimizer/identical_children_consolidation.cc b/onnxruntime/core/optimizer/identical_children_consolidation.cc index 350da9605a..bbc8073268 100644 --- a/onnxruntime/core/optimizer/identical_children_consolidation.cc +++ b/onnxruntime/core/optimizer/identical_children_consolidation.cc @@ -45,7 +45,7 @@ std::vector> IdenticalChildrenConsolidation::DivideIdenti const Graph& graph, Node* node, const string_view& op) { - unordered_map> identical_children_map; + unordered_map> identical_children_map; for (auto i = node->OutputEdgesBegin(); i != node->OutputEdgesEnd(); ++i) { if (i->GetNode().OpType() == op) { identical_children_map[IdentityBuilder(graph, i->GetNode())].push_back(i->GetNode().Index()); @@ -125,4 +125,4 @@ std::string IdenticalChildrenConsolidation::IdentityBuilder(const Graph& graph, return identity.str(); } -} // namespace onnxruntime \ No newline at end of file +} // namespace onnxruntime