r225423 - When the diagnostic text is simply "%0", sanitize the string for any

Richard Trieu rtrieu at google.com
Wed Jan 7 17:27:03 PST 2015


Author: rtrieu
Date: Wed Jan  7 19:27:03 2015
New Revision: 225423

URL: http://llvm.org/viewvc/llvm-project?rev=225423&view=rev
Log:
When the diagnostic text is simply "%0", sanitize the string for any
unprintable characters.  Fixes PR22048.

Added:
    cfe/trunk/test/Misc/diag-special-chars.c
Modified:
    cfe/trunk/lib/Basic/Diagnostic.cpp

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=225423&r1=225422&r2=225423&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Wed Jan  7 19:27:03 2015
@@ -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<char> &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

Added: cfe/trunk/test/Misc/diag-special-chars.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/diag-special-chars.c?rev=225423&view=auto
==============================================================================
--- cfe/trunk/test/Misc/diag-special-chars.c (added)
+++ cfe/trunk/test/Misc/diag-special-chars.c Wed Jan  7 19:27:03 2015
@@ -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 at -1 {{Hi   Bye}}
+
+// CHECK: error: Hi   Bye
+// CHECK: #error Hi <U+007F> <U+0080> Bye






More information about the cfe-commits mailing list