From 1eaca0d3432843d04c5da11dd5fc2c6caa72d360 Mon Sep 17 00:00:00 2001 From: faluco Date: Tue, 21 Sep 2021 13:34:08 +0200 Subject: [PATCH] Escape asn1 message strings that get printed in the enb event json report. --- lib/src/common/enb_events.cc | 49 +++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/src/common/enb_events.cc b/lib/src/common/enb_events.cc index f2d7b9119..30f19ac33 100644 --- a/lib/src/common/enb_events.cc +++ b/lib/src/common/enb_events.cc @@ -64,6 +64,46 @@ static double get_time_stamp() return std::chrono::duration_cast(tp).count() * 1e-3; } +/// Escapes the input string. +static std::string escape_string(const std::string& s) +{ + fmt::memory_buffer buff; + for (auto c : s) { + switch (c) { + case ' ': + break; + case '"': + fmt::format_to(buff, "\\\""); + break; + case '\\': + fmt::format_to(buff, "\\\\"); + break; + case '\b': + fmt::format_to(buff, "\\b"); + break; + case '\f': + fmt::format_to(buff, "\\f"); + break; + case '\n': + fmt::format_to(buff, "\\n"); + break; + case '\r': + fmt::format_to(buff, "\\r"); + break; + case '\t': + fmt::format_to(buff, "\\t"); + break; + default: + if ('\x00' <= c && c <= '\x1f') { + fmt::format_to(buff, "\\u{:04x}", c); + } else { + buff.push_back(c); + } + } + } + return fmt::to_string(buff); +} + namespace { /// Common metrics to all events. @@ -191,7 +231,8 @@ public: { rrc_event_t ctx(""); - const std::string& asn1 = (asn1_format == event_logger::asn1_output_format::octets) ? asn1_oct_str : asn1_txt_str; + const std::string& asn1 = + (asn1_format == event_logger::asn1_output_format::octets) ? asn1_oct_str : escape_string(asn1_txt_str); ctx.write("event"); ctx.write(get_time_stamp()); @@ -275,7 +316,8 @@ public: { meas_report_event_t ctx(""); - const std::string& asn1 = (asn1_format == event_logger::asn1_output_format::octets) ? asn1_oct_str : asn1_txt_str; + const std::string& asn1 = + (asn1_format == event_logger::asn1_output_format::octets) ? asn1_oct_str : escape_string(asn1_txt_str); ctx.write("event"); ctx.write(get_time_stamp()); @@ -295,7 +337,8 @@ public: { rlf_report_event_t ctx(""); - const std::string& asn1 = (asn1_format == event_logger::asn1_output_format::octets) ? asn1_oct_str : asn1_txt_str; + const std::string& asn1 = + (asn1_format == event_logger::asn1_output_format::octets) ? asn1_oct_str : escape_string(asn1_txt_str); ctx.write("event"); ctx.write(get_time_stamp());