[PATCH] D122237: [clang][lex] Fix failures with Microsoft header search rules

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 09:19:40 PDT 2022


jansvoboda11 created this revision.
Herald added a project: All.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

`HeaderSearch` currently assumes `LookupFileCache` is eventually populated in `LookupFile`. However, that's not always the case with `-fms-compatibility` and its early returns.

This patch adds a defensive check that the iterator pulled out of the cache is actually valid before using it.

(This bug was introduced in D119721 <https://reviews.llvm.org/D119721>. Before that, the cache was initialized to `0` - essentially the `search_dir_begin()` iterator.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122237

Files:
  clang/lib/Lex/HeaderSearch.cpp
  clang/test/Preprocessor/microsoft-header-search-fail.c


Index: clang/test/Preprocessor/microsoft-header-search-fail.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/microsoft-header-search-fail.c
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -Eonly -fms-compatibility %t/test.c -I %t/include -verify
+
+//--- test.c
+#include "x/header.h"
+#include "z/header.h"
+
+// expected-warning-re at include/y/header.h:1 {{#include resolved using non-portable Microsoft search rules as: {{.*}}/x/culprit.h}}
+// expected-error at include/z/header.h:1 {{'culprit.h' file not found}}
+
+//--- include/x/header.h
+#include "y/header.h"
+
+//--- include/y/header.h
+#include "culprit.h"
+
+//--- include/x/culprit.h
+
+//--- include/z/header.h
+#include "culprit.h"
Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -976,7 +976,8 @@
   // this is a matching hit.
   if (!SkipCache && CacheLookup.StartIt == NextIt) {
     // Skip querying potentially lots of directories for this lookup.
-    It = CacheLookup.HitIt;
+    if (CacheLookup.HitIt)
+      It = CacheLookup.HitIt;
     if (CacheLookup.MappedName) {
       Filename = CacheLookup.MappedName;
       if (IsMapped)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122237.417318.patch
Type: text/x-patch
Size: 1329 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220322/8eaf6196/attachment.bin>


More information about the cfe-commits mailing list