From 0d6e5b1ceabb0b53b9f31e62a69c5f310bce9fc9 Mon Sep 17 00:00:00 2001 From: Laurent Morichetti Date: Thu, 3 Nov 2022 13:49:50 -0700 Subject: [PATCH] SWDEV-362165 - Escape strings in the API function's arguments Also escape '\', '"', '\b', '\f', '\n', '\r' and '\t'. Change-Id: I02cb1a0a511156661ff40700a81b0989a450aacc --- script/gen_ostream_ops.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/script/gen_ostream_ops.py b/script/gen_ostream_ops.py index ec762c4e22..2e4e662083 100755 --- a/script/gen_ostream_ops.py +++ b/script/gen_ostream_ops.py @@ -56,11 +56,22 @@ header_basic = \ ' inline static void print_escaped_string(std::ostream& out, const char *v, size_t len) {\n' + \ ' out << \'"\'; \n' + \ ' for (size_t i = 0; i < len && v[i]; ++i) {\n' + \ -' if (std::isprint((unsigned char)v[i])) std::operator<<(out, v[i]);\n' + \ -' else {\n' + \ -' std::ios_base::fmtflags flags(out.flags());\n' + \ -' out << "\\\\x" << std::setfill(\'0\') << std::setw(2) << std::hex << (unsigned int)(unsigned char)v[i];\n' + \ -' out.flags(flags);\n' + \ +' switch (v[i]) {\n' + \ +' case \'\\"\': out << "\\\\\\""; break;\n' + \ +' case \'\\\\\': out << "\\\\\\\\"; break;\n' + \ +' case \'\\b\': out << "\\\\\\b"; break;\n' + \ +' case \'\\f\': out << "\\\\\\f"; break;\n' + \ +' case \'\\n\': out << "\\\\\\n"; break;\n' + \ +' case \'\\r\': out << "\\\\\\r"; break;\n' + \ +' case \'\\t\': out << "\\\\\\t"; break;\n' + \ +' default:\n' + \ +' if (std::isprint((unsigned char)v[i])) std::operator<<(out, v[i]);\n' + \ +' else {\n' + \ +' std::ios_base::fmtflags flags(out.flags());\n' + \ +' out << "\\\\x" << std::setfill(\'0\') << std::setw(2) << std::hex << (unsigned int)(unsigned char)v[i];\n' + \ +' out.flags(flags);\n' + \ +' }\n' + \ +' break;\n' + \ ' }\n' + \ ' }\n' + \ ' out << \'"\'; \n' + \