Skip to content

Commit ac3b92f

Browse files
committed
fix: amend review changes.
1 parent acfa7b2 commit ac3b92f

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

onnxruntime/core/providers/openvino/ov_telemetry.cc

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@ TRACELOGGING_DEFINE_PROVIDER(
2323
#pragma warning(pop)
2424
#endif
2525

26+
namespace {
27+
std::string EscapeJsonString(const std::string& input) {
28+
std::string escaped;
29+
// Reserve extra space for escaping
30+
escaped.reserve(input.size() + input.size() / 5);
31+
32+
for (char c : input) {
33+
switch (c) {
34+
case '\"': escaped += "\\\""; break;
35+
case '\\': escaped += "\\\\"; break;
36+
case '\b': escaped += "\\b"; break;
37+
case '\f': escaped += "\\f"; break;
38+
case '\n': escaped += "\\n"; break;
39+
case '\r': escaped += "\\r"; break;
40+
case '\t': escaped += "\\t"; break;
41+
default:
42+
if (c < 0x20) {
43+
escaped += "\\u";
44+
escaped += "0000"[0];
45+
escaped += "0000"[1];
46+
char hex[3];
47+
sprintf_s(hex, "%02x", static_cast<unsigned char>(c));
48+
escaped += hex;
49+
} else {
50+
escaped += c;
51+
}
52+
break;
53+
}
54+
}
55+
return escaped;
56+
}
57+
}
58+
59+
2660
namespace onnxruntime {
2761
namespace openvino_ep {
2862

@@ -91,9 +125,9 @@ void AddOptionalValue(std::ostringstream& json, const std::string& key, const T&
91125
if (!first) json << ",";
92126
json << "\"" << key << "\":";
93127
if constexpr (std::is_same_v<T, std::string>) {
94-
json << "\"" << value << "\"";
128+
json << "\"" << EscapeJsonString(value) << "\"";
95129
} else if constexpr (std::is_same_v<T, std::filesystem::path>) {
96-
json << "\"" << value.string() << "\"";
130+
json << "\"" << EscapeJsonString(value.string()) << "\"";
97131
} else if constexpr (std::is_same_v<T, bool>) {
98132
json << (value ? "true" : "false");
99133
} else {
@@ -123,13 +157,7 @@ std::string OVTelemetry::SerializeLoadConfig(const SessionContext& ctx) const {
123157
// Use ov::Any's type checking and extraction capabilities
124158
if (value.is<std::string>()) {
125159
std::string str_val = value.as<std::string>();
126-
// Escape quotes in string
127-
std::string escaped;
128-
for (char c : str_val) {
129-
if (c == '\"' || c == '\\') escaped.push_back('\\');
130-
escaped.push_back(c);
131-
}
132-
json << "\"" << escaped << "\"";
160+
json << "\"" << EscapeJsonString(str_val) << "\"";
133161
} else if (value.is<int>()) {
134162
json << value.as<int>();
135163
} else if (value.is<int64_t>()) {
@@ -147,14 +175,7 @@ std::string OVTelemetry::SerializeLoadConfig(const SessionContext& ctx) const {
147175
std::ostringstream temp;
148176
value.print(temp);
149177
std::string val_str = temp.str();
150-
151-
// Escape quotes in the printed string
152-
std::string escaped;
153-
for (char c : val_str) {
154-
if (c == '\"' || c == '\\') escaped.push_back('\\');
155-
escaped.push_back(c);
156-
}
157-
json << "\"" << escaped << "\"";
178+
json << "\"" << EscapeJsonString(val_str) << "\"";
158179
}
159180
first_entry = false;
160181
}
@@ -218,15 +239,7 @@ std::string OVTelemetry::SerializeLayoutConfig(const SessionContext& ctx) const
218239

219240
// Use ov::Layout's to_string() method for proper string representation
220241
std::string layout_str = ov_layout.to_string();
221-
222-
// Escape quotes in the layout string if any
223-
std::string escaped;
224-
for (char c : layout_str) {
225-
if (c == '\"' || c == '\\') escaped.push_back('\\');
226-
escaped.push_back(c);
227-
}
228-
229-
json << "\"" << escaped << "\"";
242+
json << "\"" << EscapeJsonString(layout_str) << "\"";
230243
first = false;
231244
}
232245
json << "}";
@@ -254,7 +267,7 @@ void OVTelemetry::LogAllProviderOptions(uint32_t session_id, const SessionContex
254267

255268
if (!ctx.cache_dir.empty()) {
256269
if (!first) opts << ",";
257-
opts << "\"cache_dir\":\"" << ctx.cache_dir.string() << "\"";
270+
opts << "\"cache_dir\":\"" << EscapeJsonString(ctx.cache_dir.string()) << "\"";
258271
first = false;
259272
}
260273

@@ -307,7 +320,7 @@ void OVTelemetry::LogAllSessionOptions(uint32_t session_id, const SessionContext
307320
// Always log model path if available
308321
if (!ctx.onnx_model_path_name.empty()) {
309322
if (!first) sopts << ",";
310-
sopts << "\"onnx_model_path_name\":\"" << ctx.onnx_model_path_name.string() << "\"";
323+
sopts << "\"onnx_model_path_name\":\"" << EscapeJsonString(ctx.onnx_model_path_name.string()) << "\"";
311324
first = false;
312325
}
313326

onnxruntime/core/providers/openvino/ov_telemetry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sstream>
1515
#include <unordered_map>
1616
#include <optional>
17+
#include <algorithm>
1718
#include "core/providers/openvino/contexts.h"
1819
#include "nlohmann/json.hpp"
1920

0 commit comments

Comments
 (0)