]> granicus.if.org Git - clang/commitdiff
[Preprocessor] Fix a crash when handling non-alpha include header.
authorHaojian Wu <hokein@google.com>
Mon, 1 Oct 2018 14:38:43 +0000 (14:38 +0000)
committerHaojian Wu <hokein@google.com>
Mon, 1 Oct 2018 14:38:43 +0000 (14:38 +0000)
Summary: the crash is casued by an assertion in StringRef.
(llvm::StringRef::front() const: Assertion `!empty()' failed.)

Reviewers: sammccall

Subscribers: jsji, cfe-commits

Differential Revision: https://reviews.llvm.org/D52721

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@343481 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/PPDirectives.cpp
test/Preprocessor/include-nonalpha-no-crash.c [new file with mode: 0644]

index 00397e1e1ac6b677a88aa82d0d780ae0134f41c6..2e20a8b9dbc9829c97d79880a9e286b984470495 100644 (file)
@@ -1889,13 +1889,16 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
       // characters
       StringRef OriginalFilename = Filename;
       if (!File) {
-        while (!isAlphanumeric(Filename.front())) {
-          Filename = Filename.drop_front();
-        }
-        while (!isAlphanumeric(Filename.back())) {
-          Filename = Filename.drop_back();
-        }
-
+        // A heuristic to correct a typo file name by removing leading and
+        // trailing non-isAlphanumeric characters.
+        auto CorrectTypoFilename = [](llvm::StringRef Filename) {
+          Filename = Filename.drop_until(isAlphanumeric);
+          while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
+            Filename = Filename.drop_back();
+          }
+          return Filename;
+        };
+        Filename = CorrectTypoFilename(Filename);
         File = LookupFile(
             FilenameLoc,
             LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
diff --git a/test/Preprocessor/include-nonalpha-no-crash.c b/test/Preprocessor/include-nonalpha-no-crash.c
new file mode 100644 (file)
index 0000000..31c1348
--- /dev/null
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 %s -verify
+
+#include "./" // expected-error {{'./' file not found}}