Skip to content

Commit 44a6e00

Browse files
authored
[clang] Check empty macro name in #pragma push_macro("") or #pragma pop_macro("") (llvm#149982)
Fixes llvm#149762. --------- Signed-off-by: yronglin <[email protected]>
1 parent 25e97fc commit 44a6e00

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ Bug Fixes in This Version
814814
- Fixed a failed assertion with an operator call expression which comes from a
815815
macro expansion when performing analysis for nullability attributes. (#GH138371)
816816
- Fixed a concept equivalent checking crash due to untransformed constraint expressions. (#GH146614)
817+
- Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
818+
``#pragma pop_macro("")``. (GH149762).
817819

818820
Bug Fixes to Compiler Builtins
819821
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ def err_pragma_push_pop_macro_malformed : Error<
694694
def warn_pragma_pop_macro_no_push : Warning<
695695
"pragma pop_macro could not pop '%0', no matching push_macro">,
696696
InGroup<IgnoredPragmas>;
697+
def warn_pargma_push_pop_macro_empty_string : Warning<
698+
"'#pragma %select{push_macro|pop_macro}0' expected a non-empty string">,
699+
InGroup<IgnoredPragmas>;
697700
def warn_pragma_message : Warning<"%0">,
698701
InGroup<PoundPragmaMessage>, DefaultWarnNoWerror;
699702
def err_pragma_message : Error<"%0">;

clang/lib/Lex/Pragma.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
591591
}
592592

593593
// Remember the macro string.
594-
std::string StrVal = getSpelling(Tok);
594+
Token StrTok = Tok;
595+
std::string StrVal = getSpelling(StrTok);
595596

596597
// Read the ')'.
597598
Lex(Tok);
@@ -604,6 +605,15 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
604605
assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
605606
"Invalid string token!");
606607

608+
if (StrVal.size() <= 2) {
609+
Diag(StrTok.getLocation(), diag::warn_pargma_push_pop_macro_empty_string)
610+
<< SourceRange(
611+
StrTok.getLocation(),
612+
StrTok.getLocation().getLocWithOffset(StrTok.getLength()))
613+
<< PragmaTok.getIdentifierInfo()->isStr("pop_macro");
614+
return nullptr;
615+
}
616+
607617
// Create a Token from the string.
608618
Token MacroTok;
609619
MacroTok.startToken();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang_cc1 -fms-extensions %s -fsyntax-only -verify
2+
3+
#pragma push_macro("") // expected-warning {{'#pragma push_macro' expected a non-empty string}}
4+
#pragma pop_macro("") // expected-warning {{'#pragma pop_macro' expected a non-empty string}}

clang/test/Preprocessor/pragma-pushpop-macro.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ int P;
5656
// CHECK: int pmy2 = 4
5757
// CHECK: int Q;
5858
// CHECK: int P;
59+
60+
#pragma push_macro("")
61+
#pragma pop_macro("")

0 commit comments

Comments
 (0)