[clang] 2ebe479 - [Clang] Disallow non-lvalue values in constant expressions to prevent invalid pointer offset computation (#95479)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 17 10:29:31 PDT 2024


Author: Oleksandr T
Date: 2024-06-17T19:29:28+02:00
New Revision: 2ebe4794b1bdb3519d5dda7ef39f32d868dfa9b0

URL: https://github.com/llvm/llvm-project/commit/2ebe4794b1bdb3519d5dda7ef39f32d868dfa9b0
DIFF: https://github.com/llvm/llvm-project/commit/2ebe4794b1bdb3519d5dda7ef39f32d868dfa9b0.diff

LOG: [Clang] Disallow non-lvalue values in constant expressions to prevent invalid pointer offset computation (#95479)

Fixes #95366

Added: 
    clang/test/Sema/integral-to-ptr.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/ExprConstant.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 69aea6c21ad39..2bf20bcd71f57 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -859,6 +859,8 @@ Bug Fixes to C++ Support
   (#GH88081), (#GH89496), (#GH90669) and (#GH91633).
 - Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
 - Clang now instantiates local constexpr functions eagerly for constant evaluators. (#GH35052), (#GH94849)
+- Fixed a failed assertion when attempting to convert an integer representing the 
diff erence
+  between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (#GH95366).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index bf9c9baf25cc6..3a6c8b4f82cca 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9325,6 +9325,13 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
       Result.IsNullPtr = false;
       return true;
     } else {
+      // In rare instances, the value isn't an lvalue.
+      // For example, when the value is the 
diff erence between the addresses of
+      // two labels. We reject that as a constant expression because we can't
+      // compute a valid offset to convert into a pointer.
+      if (!Value.isLValue())
+        return false;
+
       // Cast is of an lvalue, no need to change value.
       Result.setFrom(Info.Ctx, Value);
       return true;

diff  --git a/clang/test/Sema/integral-to-ptr.c b/clang/test/Sema/integral-to-ptr.c
new file mode 100644
index 0000000000000..b8ab4cb79820d
--- /dev/null
+++ b/clang/test/Sema/integral-to-ptr.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only -std=c11
+
+int x(void) { e: b: ; return &&e - &&b < x; } // expected-warning {{ordered comparison between pointer and integer}}


        


More information about the cfe-commits mailing list