Skip to content

Conversation

@Fznamznon
Copy link
Contributor

If a negative value was given we would fail to skip till the end of the directive and trip a failed assertion.

Fixes #157842

If a negative value was given we would fail to skip till the end of the
directive and trip a failed assertion.

Fixes llvm#157842
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-clang

Author: Mariya Podchishchaeva (Fznamznon)

Changes

If a negative value was given we would fail to skip till the end of the directive and trip a failed assertion.

Fixes #157842


Full diff: https://github.com/llvm/llvm-project/pull/157896.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Lex/PPDirectives.cpp (+2)
  • (modified) clang/test/Preprocessor/embed___has_embed_parsing_errors.c (+9)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a20b1ab298f9c..b5009764f583f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -308,6 +308,8 @@ Bug Fixes in This Version
 - Builtin elementwise operators now accept vector arguments that have different
   qualifiers on their elements. For example, vector of 4 ``const float`` values
   and vector of 4 ``float`` values. (#GH155405)
+- Fixed a failed assertion with a negative limit parameter value inside of
+  ``__has_embed``. (#GH157842)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 9d01b8d99e227..360593d0f33df 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3746,6 +3746,8 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
       if (Result.isNegative()) {
         Diag(CurTok, diag::err_requires_positive_value)
             << toString(Result, 10) << /*positive*/ 0;
+        if (CurTok.isNot(tok::eod))
+          DiscardUntilEndOfDirective(CurTok);
         return std::nullopt;
       }
       return Result.getLimitedValue();
diff --git a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
index fcaf693fe0ff2..0591c595253bc 100644
--- a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
+++ b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
@@ -238,3 +238,12 @@
 #if __has_embed("media/art.txt" if_empty))
 #endif
 
+// expected-error@+2 {{invalid value '-1'; must be positive}} \
+   expected-error@+2 {{expected value in expression}}
+#if __has_embed (__FILE__ limit(-1))
+#endif
+
+// expected-error@+2 {{invalid value '-100000000000000000'; must be positive}}\
+   expected-error@+2 {{expected value in expression}}
+#if __has_embed (__FILE__ limit(-100000000000000000)) != __STDC_EMBED_NOT_FOUND__
+#endif

@Fznamznon Fznamznon changed the title [clang] Fix assertion with invalid limit parameter value [clang] Fix assertion with invalid embed limit parameter value Sep 10, 2025
@Fznamznon Fznamznon added the embed #embed (not embedded systems) label Sep 10, 2025
@Fznamznon Fznamznon merged commit 98f1ae0 into llvm:main Sep 11, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category embed #embed (not embedded systems)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] Assertion `(Params || Tok.is(tok::eod)) && "expected success or to be at the end of the directive"' failed.

3 participants