[clang] [clang] Fix a segfault when M is a nullptr (PR #130712)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 16 23:19:43 PDT 2025


https://github.com/matts1 updated https://github.com/llvm/llvm-project/pull/130712

>From 2c0dc1e7ee6a7ec499f4fcb88c79dc1aff8ce2ca Mon Sep 17 00:00:00 2001
From: Matt Stark <msta at google.com>
Date: Mon, 10 Mar 2025 13:07:29 +1100
Subject: [PATCH] [clang] Fix a segfault when M is a nullptr

---
 clang/docs/ReleaseNotes.rst        |  1 +
 clang/include/clang/Basic/Module.h |  2 +-
 clang/test/Modules/pr130712.cppm   | 31 ++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Modules/pr130712.cppm

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2a1c5ee2d788e..c7f8a564fe63a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -318,6 +318,7 @@ Bug Fixes to C++ Support
 - Fixed an assertion failure affecting code that uses C++23 "deducing this". (#GH130272)
 - Clang now properly instantiates destructors for initialized members within non-delegating constructors. (#GH93251)
 - Correctly diagnoses if unresolved using declarations shadows template paramters (#GH129411)
+- Clang no longer segfaults when there is a configuration mismatch between modules and their users (http://crbug.com/400353616).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h
index 62cc8acf9588b..3d035f0a5f787 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -888,7 +888,7 @@ class VisibleModuleSet {
 
   /// Get the location at which the import of a module was triggered.
   SourceLocation getImportLoc(const Module *M) const {
-    return M->getVisibilityID() < ImportLocs.size()
+    return M && M->getVisibilityID() < ImportLocs.size()
                ? ImportLocs[M->getVisibilityID()]
                : SourceLocation();
   }
diff --git a/clang/test/Modules/pr130712.cppm b/clang/test/Modules/pr130712.cppm
new file mode 100644
index 0000000000000..f908f1cb50bca
--- /dev/null
+++ b/clang/test/Modules/pr130712.cppm
@@ -0,0 +1,31 @@
+// RUN: split-file %s %t
+
+// There are two requirements here to result in the owner of a macro being null.
+// 1) There must be a configuration mismatch between a header and a file it depends on
+// 2) -fmodules-local-submodule-visibility must be enabled.
+
+// RUN: %clang_cc1 -I%t -emit-module -o %t/a.pcm -fmodules %t/module.modulemap -fmodule-name=a -fmodules-local-submodule-visibility 
+// RUN: %clang_cc1 -fexceptions -Wno-module-file-config-mismatch -I%t -emit-module -o %t/b.pcm -fmodules %t/module.modulemap -fmodule-name=b -fmodules-local-submodule-visibility -fmodule-file=%t/a.pcm
+// RUN: %clang_cc1 -fexceptions -Wno-module-file-config-mismatch -I%t -emit-module -o %t/c.pcm -fmodules %t/module.modulemap -fmodule-name=c -fmodules-local-submodule-visibility -fmodule-file=%t/a.pcm -fmodule-file=%t/b.pcm
+
+//--- module.modulemap
+module a { header "a.h" }
+module b { header "b.h" }
+module c { header "c.h" }
+
+//--- a.h
+#ifndef A_H
+#define A_H
+#endif
+
+//--- b.h
+#ifndef B_H
+#define B_H
+
+#include <a.h>
+
+#endif
+
+//--- c.h
+#include <a.h>
+#include <b.h>



More information about the cfe-commits mailing list