From: Richard Trieu Date: Thu, 8 Jan 2015 01:27:03 +0000 (+0000) Subject: When the diagnostic text is simply "%0", sanitize the string for any X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c96cf442c306153b49747964817fa9ac8ec8e95b;p=clang When the diagnostic text is simply "%0", sanitize the string for any unprintable characters. Fixes PR22048. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@225423 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp index a892946608..83228ad3c5 100644 --- a/lib/Basic/Diagnostic.cpp +++ b/lib/Basic/Diagnostic.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/CrashRecoveryContext.h" +#include "llvm/Support/Locale.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -629,6 +630,20 @@ void Diagnostic:: FormatDiagnostic(const char *DiagStr, const char *DiagEnd, SmallVectorImpl &OutStr) const { + // When the diagnostic string is only "%0", the entire string is being given + // by an outside source. Remove unprintable characters from this string + // and skip all the other string processing. + if (DiagEnd - DiagStr == 2 && DiagStr[0] == '%' && DiagStr[1] == '0' && + getArgKind(0) == DiagnosticsEngine::ak_std_string) { + const std::string &S = getArgStdStr(0); + for (char c : S) { + if (llvm::sys::locale::isPrint(c) || c == '\t') { + OutStr.push_back(c); + } + } + return; + } + /// FormattedArgs - Keep track of all of the arguments formatted by /// ConvertArgToString and pass them into subsequent calls to /// ConvertArgToString, allowing the implementation to avoid redundancies in diff --git a/test/Misc/diag-special-chars.c b/test/Misc/diag-special-chars.c new file mode 100644 index 0000000000..007f9d04f0 --- /dev/null +++ b/test/Misc/diag-special-chars.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 %s -verify +// RUN: not %clang_cc1 %s 2>&1 | FileCheck %s + +// There are two special characters on the following line, one which is used +// as a marker character for diagnostic printing. Ensure diagnostics involving +// these characters do not cause problems with the diagnostic printer. +#error Hi  € Bye +//expected-error@-1 {{Hi Bye}} + +// CHECK: error: Hi Bye +// CHECK: #error Hi Bye