Skip to content

Commit 519420f

Browse files
authored
[core] Fix regex out of memory error (#29403)
### Details: - Replace regex usage by standard iteration as on MSVC is not working correctly ### Tickets: - EISW-157577 --------- Signed-off-by: Raasz, Pawel <pawel.raasz@intel.com>
1 parent 9788721 commit 519420f

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/frontends/ir/src/ir_deserializer.cpp

+22-10
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,30 @@ namespace {
4444
* @return A set of unique tensor names.
4545
*/
4646
std::unordered_set<std::string> deserialize_tensor_names(const std::string_view& tensor_names) {
47-
// tensor names are separated by comma, but ignore escaped comma
48-
static const auto splitter = std::regex(R"((?:[^\\,\n]|\\.)+)");
47+
static const auto escaped_delim = std::regex(R"(\\,)");
48+
constexpr auto delim = ",";
49+
constexpr auto esc_char = '\\';
4950

5051
auto output_names = std::unordered_set<std::string>();
51-
std::transform(std::cregex_token_iterator{tensor_names.data(), tensor_names.data() + tensor_names.size(), splitter},
52-
std::cregex_token_iterator{},
53-
std::inserter(output_names, output_names.end()),
54-
[](const auto& token) {
55-
// If tensor name contains escaped comma, replace it with comma
56-
static const auto escaped_delim = std::regex(R"(\\,)");
57-
return std::regex_replace(token.str(), escaped_delim, ",");
58-
});
52+
auto name_inserter = std::inserter(output_names, output_names.end());
53+
for (size_t pos = tensor_names.find(delim), start = 0; start != std::string::npos;
54+
pos = tensor_names.find(delim, pos)) {
55+
if (pos == std::string::npos) {
56+
if (auto name_view = tensor_names.substr(start); name_view.size() > 0) {
57+
*name_inserter = std::regex_replace(std::string(name_view), escaped_delim, delim);
58+
}
59+
start = pos;
60+
} else if (auto delim_pos = pos - 1; delim_pos != std::string::npos && tensor_names[delim_pos] == esc_char) {
61+
++pos;
62+
} else {
63+
if (auto length = pos - start; length > 0) {
64+
*name_inserter =
65+
std::regex_replace(std::string(tensor_names.substr(start, length)), escaped_delim, delim);
66+
}
67+
start = ++pos;
68+
}
69+
}
70+
5971
return output_names;
6072
}
6173
} // namespace

src/frontends/ir/tests/frontend_test_basic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ TEST_F(IRFrontendTests, model_with_tensor_names_with_spaces) {
13031303
<layer id="0" name="input2" type="Parameter" version="opset1">
13041304
<data shape="1,4,512" element_type="f32"/>
13051305
<output>
1306-
<port id="0" precision="FP32" names="input2">
1306+
<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">
13071307
<dim>1</dim>
13081308
<dim>4</dim>
13091309
<dim>512</dim>

0 commit comments

Comments
 (0)