https://github.com/Fznamznon created 
https://github.com/llvm/llvm-project/pull/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

>From 08342b79112f25748b95b0a5983703ffe1accb60 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <[email protected]>
Date: Wed, 10 Sep 2025 09:47:44 -0700
Subject: [PATCH] [clang] Fix assertion with invalid limit parameter value

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
---
 clang/docs/ReleaseNotes.rst                              | 2 ++
 clang/lib/Lex/PPDirectives.cpp                           | 2 ++
 .../test/Preprocessor/embed___has_embed_parsing_errors.c | 9 +++++++++
 3 files changed, 13 insertions(+)

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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to