Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Fix regex out of memory error (#29403)

Pawel Raasz519420f66149
diff --git a/src/frontends/ir/src/ir_deserializer.cpp b/src/frontends/ir/src/ir_deserializer.cpp
index e3defea8e73..b553473caa5 100644
--- a/src/frontends/ir/src/ir_deserializer.cpp
+++ b/src/frontends/ir/src/ir_deserializer.cpp
@@ -44,18 +44,30 @@ namespace {
* @return A set of unique tensor names.
*/
std::unordered_set<std::string> deserialize_tensor_names(const std::string_view& tensor_names) {
- // tensor names are separated by comma, but ignore escaped comma
- static const auto splitter = std::regex(R"((?:[^\\,\n]|\\.)+)");
+ static const auto escaped_delim = std::regex(R"(\\,)");
+ constexpr auto delim = ",";
+ constexpr auto esc_char = '\\';
auto output_names = std::unordered_set<std::string>();
- std::transform(std::cregex_token_iterator{tensor_names.data(), tensor_names.data() + tensor_names.size(), splitter},
- std::cregex_token_iterator{},
- std::inserter(output_names, output_names.end()),
- [](const auto& token) {
- // If tensor name contains escaped comma, replace it with comma
- static const auto escaped_delim = std::regex(R"(\\,)");
- return std::regex_replace(token.str(), escaped_delim, ",");
- });
+ auto name_inserter = std::inserter(output_names, output_names.end());
+ for (size_t pos = tensor_names.find(delim), start = 0; start != std::string::npos;
+ pos = tensor_names.find(delim, pos)) {
+ if (pos == std::string::npos) {
+ if (auto name_view = tensor_names.substr(start); name_view.size() > 0) {
+ *name_inserter = std::regex_replace(std::string(name_view), escaped_delim, delim);
+ }
+ start = pos;
+ } else if (auto delim_pos = pos - 1; delim_pos != std::string::npos && tensor_names[delim_pos] == esc_char) {
+ ++pos;
+ } else {
+ if (auto length = pos - start; length > 0) {
+ *name_inserter =
+ std::regex_replace(std::string(tensor_names.substr(start, length)), escaped_delim, delim);
+ }
+ start = ++pos;
+ }
+ }
+
return output_names;
}
} // namespace
diff --git a/src/frontends/ir/tests/frontend_test_basic.cpp b/src/frontends/ir/tests/frontend_test_basic.cpp
index c63b57e3d51..f377502e3a2 100644
--- a/src/frontends/ir/tests/frontend_test_basic.cpp
+++ b/src/frontends/ir/tests/frontend_test_basic.cpp
@@ -1303,7 +1303,7 @@ TEST_F(IRFrontendTests, model_with_tensor_names_with_spaces) {
<layer id="0" name="input2" type="Parameter" version="opset1">
<data shape="1,4,512" element_type="f32"/>
<output>
- <port id="0" precision="FP32" names="input2">
+ <port id="0" precision="FP32" names="model/bert/encoder/layer_0/attention/self/query/Tensordot/MatMul;model/bert/encoder/layer_0/attention/self/query/BiasAdd;model/bert/encoder/layer_0/attention/output/dense/Tensordot/shape;model/bert/encoder/layer_0/attention/self/query/Tensordot;model/bert/encoder/layer_0/attention/self/query/BiasAdd/ReadVariableOp_Gemm__32:0">
<dim>1</dim>
<dim>4</dim>
<dim>512</dim>