From bf7e2b81e779713cc535c884e287ec6cf02fcfef Mon Sep 17 00:00:00 2001 From: Oleksandr Tarasiuk Date: Wed, 28 Jan 2026 15:44:48 +0200 Subject: [PATCH] [Clang] prevent preprocessor crash on incomplete scoped __has_cpp_attribute arguments (#178273) Fixes #178098 --- This patch addressed the issue when `__has_cpp_attribute` is expanded with incomplete scoped attributes. The scoped name parsing can lex to `eof`/`eod` at https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1877-L1881 and then proceed with https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1425-L1430 since `eof`/`eod` is not guarded at https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1367-L1372 this could lead to a preprocessor crash https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1370 --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Lex/PPMacroExpansion.cpp | 4 ++-- .../test/Preprocessor/has_attribute_errors.cpp | 17 +++++++++++++++++ .../test/Preprocessor/has_c_attribute_errors.c | 9 +++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 clang/test/Preprocessor/has_c_attribute_errors.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index b5327c01fbb8..42a300ca9458 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -210,6 +210,7 @@ Bug Fixes in This Version - Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters are missing parentheses. (#GH175088) - Fix lifetime extension of temporaries in for-range-initializers in templates. (#GH165182) +- Fixed a preprocessor crash in ``__has_cpp_attribute`` on incomplete scoped attributes. (#GH178098) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 5efa4b5b3f87..350bd6c88eaa 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1364,7 +1364,7 @@ static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS, Token ResultTok; bool SuppressDiagnostic = false; - while (true) { + while (Tok.isNoneOf(tok::eod, tok::eof)) { // Parse next token. if (ExpandArgs) PP.Lex(Tok); @@ -1443,7 +1443,7 @@ already_lexed: PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; SuppressDiagnostic = true; } - } +} } /// Helper function to return the IdentifierInfo structure of a Token diff --git a/clang/test/Preprocessor/has_attribute_errors.cpp b/clang/test/Preprocessor/has_attribute_errors.cpp index 1fc88d3f926f..199227e90f05 100644 --- a/clang/test/Preprocessor/has_attribute_errors.cpp +++ b/clang/test/Preprocessor/has_attribute_errors.cpp @@ -14,3 +14,20 @@ __has_cpp_attribute(__clang__::fallthrough) // expected-error {{missing ')' afte // expected-note {{to match this '('}} \ // expected-error {{builtin feature check macro requires a parenthesized identifier}} +namespace GH178098 { +// expected-error@+2 {{builtin feature check macro requires a parenthesized identifier}} +// expected-error@+1 {{expected value in expression}} +#if __has_cpp_attribute(clang:: +#endif + +// expected-error@+4 {{unterminated function-like macro invocation}} +// expected-error@+3 {{missing ')' after 'clang'}} +// expected-error@+2 {{expected value in expression}} +// expected-note@+1 {{to match this '('}} +#if __has_attribute(clang:: +#endif + +// expected-error@+3 {{builtin feature check macro requires a parenthesized identifier}} +// expected-error@+2 {{unterminated function-like macro invocation}} +__has_cpp_attribute(clang:: +} diff --git a/clang/test/Preprocessor/has_c_attribute_errors.c b/clang/test/Preprocessor/has_c_attribute_errors.c new file mode 100644 index 000000000000..026c0bd37e7f --- /dev/null +++ b/clang/test/Preprocessor/has_c_attribute_errors.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple i386-unknown-unknown -Eonly -verify %s + +// expected-error@+2 {{builtin feature check macro requires a parenthesized identifier}} +// expected-error@+1 {{expected value in expression}} +#if __has_c_attribute(clang:: +#endif + +// expected-error@+1 {{builtin feature check macro requires a parenthesized identifier}} +__has_c_attribute(clang::