From: Csaba Dabis Date: Tue, 25 Jun 2019 03:08:32 +0000 (+0000) Subject: [analyzer] JsonSupport: Escape escapes X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6474722055ae59e915fd647ad17a58c2b95618e4;p=clang [analyzer] JsonSupport: Escape escapes Summary: - Reviewers: NoQ Reviewed By: NoQ Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63462 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@364270 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/JsonSupport.h b/include/clang/Basic/JsonSupport.h index f235daa168..a3d12cbec6 100644 --- a/include/clang/Basic/JsonSupport.h +++ b/include/clang/Basic/JsonSupport.h @@ -31,7 +31,26 @@ inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) { std::string Str = RawSR.trim().str(); size_t Pos = 0; + // Escape backslashes. + while (true) { + Pos = Str.find('\\', Pos); + if (Pos == std::string::npos) + break; + + // Prevent bad conversions. + size_t TempPos = (Pos != 0) ? Pos - 1 : 0; + + // See whether the current backslash is not escaped. + if (TempPos != Str.find("\\\\", Pos)) { + Str.insert(Pos, "\\"); + ++Pos; // As we insert the backslash move plus one. + } + + ++Pos; + } + // Escape double quotes. + Pos = 0; while (true) { Pos = Str.find('\"', Pos); if (Pos == std::string::npos) @@ -40,8 +59,8 @@ inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) { // Prevent bad conversions. size_t TempPos = (Pos != 0) ? Pos - 1 : 0; - // See whether the current double quote is escaped. - if (TempPos != Str.find("\\\"", TempPos)) { + // See whether the current double quote is not escaped. + if (TempPos != Str.find("\\\"", Pos)) { Str.insert(Pos, "\\"); ++Pos; // As we insert the escape-character move plus one. } diff --git a/test/Analysis/dump_egraph.c b/test/Analysis/dump_egraph.c index 5544d9fae2..1134375303 100644 --- a/test/Analysis/dump_egraph.c +++ b/test/Analysis/dump_egraph.c @@ -13,6 +13,8 @@ int getJ(); int foo() { int *x = 0, *y = 0; + char c = '\x13'; + return *x + *y; } @@ -22,5 +24,7 @@ int foo() { // CHECK: \"has_report\": true -// CHECK: \"pretty\": \"*x\", \"location\": \{ \"line\": 16, \"column\": 10, \"file\": \"{{(.+)}}dump_egraph.c\" \} +// CHECK: \"pretty\": \"*x\", \"location\": \{ \"line\": 18, \"column\": 10, \"file\": \"{{(.+)}}dump_egraph.c\" \} + +// CHECK: \"pretty\": \"'\\\\x13'\" diff --git a/test/Analysis/exploded-graph-rewriter/escapes.c b/test/Analysis/exploded-graph-rewriter/escapes.c index 32a7713561..140ec676fb 100644 --- a/test/Analysis/exploded-graph-rewriter/escapes.c +++ b/test/Analysis/exploded-graph-rewriter/escapes.c @@ -15,7 +15,7 @@ void escapes() { // CHECK: Environment: // CHECK-SAME: "foo" // CHECK-SAME: &Element\{"foo",0 S64b,char\} - const char *const foo = "foo"; + const char *const foo = "\x66\x6f\x6f"; // CHECK: BinaryOperator // CHECK-SAME: 1 \| 2