[clang] 98f1ae0 - [clang] Fix assertion with invalid embed limit parameter value (#157896)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 11 01:49:47 PDT 2025


Author: Mariya Podchishchaeva
Date: 2025-09-11T10:49:43+02:00
New Revision: 98f1ae057b8b829bdc18fba4b5209b5aceb5cf80

URL: https://github.com/llvm/llvm-project/commit/98f1ae057b8b829bdc18fba4b5209b5aceb5cf80
DIFF: https://github.com/llvm/llvm-project/commit/98f1ae057b8b829bdc18fba4b5209b5aceb5cf80.diff

LOG: [clang] Fix assertion with invalid embed limit parameter value (#157896)

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

Fixes https://github.com/llvm/llvm-project/issues/157842

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Lex/PPDirectives.cpp
    clang/test/Preprocessor/embed___has_embed_parsing_errors.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c0e3fafc379c6..086ad2e3a92eb 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 
diff erent
   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 at +2 {{invalid value '-1'; must be positive}} \
+   expected-error at +2 {{expected value in expression}}
+#if __has_embed (__FILE__ limit(-1))
+#endif
+
+// expected-error at +2 {{invalid value '-100000000000000000'; must be positive}}\
+   expected-error at +2 {{expected value in expression}}
+#if __has_embed (__FILE__ limit(-100000000000000000)) != __STDC_EMBED_NOT_FOUND__
+#endif


        


More information about the cfe-commits mailing list