[PATCH] D146156: [clang][Lexer] Fix crash/assert clang::HeaderSearch::search_dir_nth

Dmitry Polukhin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 16 02:31:55 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGb293c6280d06: [clang][Lexer] Fix crash/assert clang::HeaderSearch::search_dir_nth (authored by DmitryPolukhin).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146156/new/

https://reviews.llvm.org/D146156

Files:
  clang/lib/Lex/HeaderSearch.cpp
  clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json
  clang/test/Preprocessor/header-search-crash.c


Index: clang/test/Preprocessor/header-search-crash.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/header-search-crash.c
@@ -0,0 +1,5 @@
+// RUN: rm -rf %t && mkdir %t
+// RUN: %hmaptool write %S/Inputs/header-search-crash/foo.hmap.json %t/foo.hmap
+// RUN: %clang -cc1 -E %s -I %t/foo.hmap -verify
+
+#include "MissingHeader.h" // expected-error {{'MissingHeader.h' file not found}}
Index: clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json
@@ -0,0 +1,6 @@
+{
+  "mappings" :
+    {
+     "Foo.h" : "Foo/Foo.h"
+    }
+}
Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -378,15 +378,17 @@
   llvm::StringMap<unsigned, llvm::BumpPtrAllocator> Index(SearchDirs.size());
 
   // Iterate over all filename keys and associate them with the index i.
-  unsigned i = 0;
-  for (; i != SearchDirs.size(); ++i) {
+  for (unsigned i = 0; i != SearchDirs.size(); ++i) {
     auto &Dir = SearchDirs[i];
 
     // We're concerned with only the initial contiguous run of header
     // maps within SearchDirs, which can be 99% of SearchDirs when
     // SearchDirs.size() is ~10000.
-    if (!Dir.isHeaderMap())
+    if (!Dir.isHeaderMap()) {
+      SearchDirHeaderMapIndex = std::move(Index);
+      FirstNonHeaderMapSearchDirIdx = i;
       break;
+    }
 
     // Give earlier keys precedence over identical later keys.
     auto Callback = [&](StringRef Filename) {
@@ -394,9 +396,6 @@
     };
     Dir.getHeaderMap()->forEachKey(Callback);
   }
-
-  SearchDirHeaderMapIndex = std::move(Index);
-  FirstNonHeaderMapSearchDirIdx = i;
 }
 
 //===----------------------------------------------------------------------===//
@@ -1929,7 +1928,7 @@
     llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
     bool *IsSystem) {
   using namespace llvm::sys;
-  
+
   llvm::SmallString<32> FilePath = File;
   // remove_dots switches to backslashes on windows as a side-effect!
   // We always want to suggest forward slashes for includes.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146156.505736.patch
Type: text/x-patch
Size: 2307 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230316/75332386/attachment-0001.bin>


More information about the cfe-commits mailing list