[clang] [clang] Check empty macro name in `#pragma push_macro("")` or `#pragma pop_macro("")` (PR #149982)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 22 05:37:52 PDT 2025
https://github.com/yronglin updated https://github.com/llvm/llvm-project/pull/149982
>From d7f00bde68176aab93d4da50a12aea00c29d5276 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Tue, 22 Jul 2025 17:18:36 +0800
Subject: [PATCH 1/2] [clang] Check empty macro name in #pragma push_macro("")
or #pragma pop_macro("")
Signed-off-by: yronglin <yronglin777 at gmail.com>
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Lex/Pragma.cpp | 4 ++++
clang/test/Preprocessor/pragma-pushpop-macro.c | 3 +++
3 files changed, 9 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 81483c12c8fe9..461902701bc48 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -814,6 +814,8 @@ Bug Fixes in This Version
- Fixed a failed assertion with an operator call expression which comes from a
macro expansion when performing analysis for nullability attributes. (#GH138371)
- Fixed a concept equivalent checking crash due to untransformed constraint expressions. (#GH146614)
+- Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
+ ``#pragma pop_macro("")``. (GH149762).
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 01c85e6ad95d5..7f1190c4dcce6 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -604,6 +604,10 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!");
+ // FIXME: Should we emit a warning?
+ if (StrVal.size() <= 2)
+ return nullptr;
+
// Create a Token from the string.
Token MacroTok;
MacroTok.startToken();
diff --git a/clang/test/Preprocessor/pragma-pushpop-macro.c b/clang/test/Preprocessor/pragma-pushpop-macro.c
index 0aee074c55c77..238e3ed5eddb3 100644
--- a/clang/test/Preprocessor/pragma-pushpop-macro.c
+++ b/clang/test/Preprocessor/pragma-pushpop-macro.c
@@ -56,3 +56,6 @@ int P;
// CHECK: int pmy2 = 4
// CHECK: int Q;
// CHECK: int P;
+
+#pragma push_macro("")
+#pragma pop_macro("")
>From b94ebcfdbb208e2bbd17d2e7002cf012b1086e31 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Tue, 22 Jul 2025 20:36:50 +0800
Subject: [PATCH 2/2] Add diagnostic
Signed-off-by: yronglin <yronglin777 at gmail.com>
---
clang/include/clang/Basic/DiagnosticLexKinds.td | 3 +++
clang/lib/Lex/Pragma.cpp | 12 +++++++++---
clang/test/Preprocessor/pragma-pushpop-macro-diag.c | 4 ++++
3 files changed, 16 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Preprocessor/pragma-pushpop-macro-diag.c
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 723f5d48b4f5f..73d5e51da1fd7 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -694,6 +694,9 @@ def err_pragma_push_pop_macro_malformed : Error<
def warn_pragma_pop_macro_no_push : Warning<
"pragma pop_macro could not pop '%0', no matching push_macro">,
InGroup<IgnoredPragmas>;
+def warn_pargma_push_pop_macro_empty_string : Warning<
+ "#pragma %select{push_macro|pop_macro}0 expected an non-empty string">,
+ InGroup<IgnoredPragmas>;
def warn_pragma_message : Warning<"%0">,
InGroup<PoundPragmaMessage>, DefaultWarnNoWerror;
def err_pragma_message : Error<"%0">;
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 7f1190c4dcce6..bba3c89bed38f 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -591,7 +591,8 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
}
// Remember the macro string.
- std::string StrVal = getSpelling(Tok);
+ Token StrTok = Tok;
+ std::string StrVal = getSpelling(StrTok);
// Read the ')'.
Lex(Tok);
@@ -604,9 +605,14 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!");
- // FIXME: Should we emit a warning?
- if (StrVal.size() <= 2)
+ if (StrVal.size() <= 2) {
+ Diag(StrTok.getLocation(), diag::warn_pargma_push_pop_macro_empty_string)
+ << SourceRange(
+ StrTok.getLocation(),
+ StrTok.getLocation().getLocWithOffset(StrTok.getLength()))
+ << PragmaTok.getIdentifierInfo()->isStr("pop_macro");
return nullptr;
+ }
// Create a Token from the string.
Token MacroTok;
diff --git a/clang/test/Preprocessor/pragma-pushpop-macro-diag.c b/clang/test/Preprocessor/pragma-pushpop-macro-diag.c
new file mode 100644
index 0000000000000..9878b96938216
--- /dev/null
+++ b/clang/test/Preprocessor/pragma-pushpop-macro-diag.c
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -fms-extensions %s -fsyntax-only -verify
+
+#pragma push_macro("") // expected-warning {{#pragma push_macro expected an non-empty string}}
+#pragma pop_macro("") // expected-warning {{#pragma pop_macro expected an non-empty string}}
More information about the cfe-commits
mailing list