[PATCH] D52721: [Preprocessor] Fix a crash when handling non-alpha include header.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 05:12:24 PDT 2018


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a subscriber: jsji.

This also fixes a crash of code completion on `#include "./^"`.


Repository:
  rC Clang

https://reviews.llvm.org/D52721

Files:
  lib/Lex/PPDirectives.cpp
  test/Preprocessor/include-nonalpha-no-crash.c


Index: test/Preprocessor/include-nonalpha-no-crash.c
===================================================================
--- /dev/null
+++ test/Preprocessor/include-nonalpha-no-crash.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 %s -verify
+
+#include "./" // expected-error {{'./' file not found}}
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp
+++ lib/Lex/PPDirectives.cpp
@@ -1889,27 +1889,29 @@
       // characters
       StringRef OriginalFilename = Filename;
       if (!File) {
-        while (!isAlphanumeric(Filename.front())) {
+        while (!Filename.empty() && !isAlphanumeric(Filename.front())) {
           Filename = Filename.drop_front();
         }
-        while (!isAlphanumeric(Filename.back())) {
+        while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
           Filename = Filename.drop_back();
         }
 
-        File = LookupFile(
-            FilenameLoc,
-            LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
-            LookupFrom, LookupFromFile, CurDir,
-            Callbacks ? &SearchPath : nullptr,
-            Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
-        if (File) {
-          SourceRange Range(FilenameTok.getLocation(), CharEnd);
-          auto Hint = isAngled ? FixItHint::CreateReplacement(
-                                     Range, "<" + Filename.str() + ">")
-                               : FixItHint::CreateReplacement(
-                                     Range, "\"" + Filename.str() + "\"");
-          Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
-              << OriginalFilename << Filename << Hint;
+        if (!Filename.empty()) {
+          File = LookupFile(
+              FilenameLoc,
+              LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
+              LookupFrom, LookupFromFile, CurDir,
+              Callbacks ? &SearchPath : nullptr,
+              Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
+          if (File) {
+            SourceRange Range(FilenameTok.getLocation(), CharEnd);
+            auto Hint = isAngled ? FixItHint::CreateReplacement(
+                                      Range, "<" + Filename.str() + ">")
+                                : FixItHint::CreateReplacement(
+                                      Range, "\"" + Filename.str() + "\"");
+            Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
+                << OriginalFilename << Filename << Hint;
+          }
         }
       }
 
@@ -1920,6 +1922,9 @@
     }
   }
 
+  if (Filename.empty())
+    return;
+
   if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
     if (isPCHThroughHeader(File))
       SkippingUntilPCHThroughHeader = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52721.167704.patch
Type: text/x-patch
Size: 2855 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181001/f5934f8f/attachment-0001.bin>


More information about the cfe-commits mailing list