r343481 - [Preprocessor] Fix a crash when handling non-alpha include header.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 07:38:43 PDT 2018


Author: hokein
Date: Mon Oct  1 07:38:43 2018
New Revision: 343481

URL: http://llvm.org/viewvc/llvm-project?rev=343481&view=rev
Log:
[Preprocessor] Fix a crash when handling non-alpha include header.

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

Added:
    cfe/trunk/test/Preprocessor/include-nonalpha-no-crash.c
Modified:
    cfe/trunk/lib/Lex/PPDirectives.cpp

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=343481&r1=343480&r2=343481&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Mon Oct  1 07:38:43 2018
@@ -1889,13 +1889,16 @@ void Preprocessor::HandleIncludeDirectiv
       // 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,

Added: cfe/trunk/test/Preprocessor/include-nonalpha-no-crash.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/include-nonalpha-no-crash.c?rev=343481&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/include-nonalpha-no-crash.c (added)
+++ cfe/trunk/test/Preprocessor/include-nonalpha-no-crash.c Mon Oct  1 07:38:43 2018
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 %s -verify
+
+#include "./" // expected-error {{'./' file not found}}




More information about the cfe-commits mailing list