Author: Dmitry Polukhin Date: 2023-03-16T02:19:11-07:00 New Revision: b293c6280d06f49c5ca7290855911341ab0bdffa
URL: https://github.com/llvm/llvm-project/commit/b293c6280d06f49c5ca7290855911341ab0bdffa DIFF: https://github.com/llvm/llvm-project/commit/b293c6280d06f49c5ca7290855911341ab0bdffa.diff LOG: [clang][Lexer] Fix crash/assert clang::HeaderSearch::search_dir_nth The issue was introduced in D135801. When there are only header maps in the SearchDirs, the out of bounds value is assigned to FirstNonHeaderMapSearchDirIdx. Test Plan: check-clang Differential Revision: https://reviews.llvm.org/D146156 Added: clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json clang/test/Preprocessor/header-search-crash.c Modified: clang/lib/Lex/HeaderSearch.cpp Removed: ################################################################################ diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 074c147ba3c54..5a7357a5ada43 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -378,15 +378,17 @@ void HeaderSearch::indexInitialHeaderMaps() { 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 @@ void HeaderSearch::indexInitialHeaderMaps() { }; Dir.getHeaderMap()->forEachKey(Callback); } - - SearchDirHeaderMapIndex = std::move(Index); - FirstNonHeaderMapSearchDirIdx = i; } //===----------------------------------------------------------------------===// @@ -1929,7 +1928,7 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics( 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. diff --git a/clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json b/clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json new file mode 100644 index 0000000000000..ccfd911f0f7fd --- /dev/null +++ b/clang/test/Preprocessor/Inputs/header-search-crash/foo.hmap.json @@ -0,0 +1,6 @@ +{ + "mappings" : + { + "Foo.h" : "Foo/Foo.h" + } +} diff --git a/clang/test/Preprocessor/header-search-crash.c b/clang/test/Preprocessor/header-search-crash.c new file mode 100644 index 0000000000000..8c04216d18ba1 --- /dev/null +++ b/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}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits