[clang] [Clang] fix(95366): enhance cast operation safety with LValue validation (PR #95479)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 14 08:17:42 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/95479

>From af6ba760826467a3ce7b7a41c18f194528a1cede Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 14 Jun 2024 01:26:34 +0300
Subject: [PATCH 1/4] [Clang] fix(95366): enhance cast operation safety with
 LValue validation

---
 clang/docs/ReleaseNotes.rst       | 1 +
 clang/lib/AST/ExprConstant.cpp    | 3 +++
 clang/test/Sema/integral-to-ptr.c | 3 +++
 3 files changed, 7 insertions(+)
 create mode 100644 clang/test/Sema/integral-to-ptr.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8c2f737836a9d..755557906360b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -847,6 +847,7 @@ Bug Fixes to C++ Support
 - Fixed several bugs in capturing variables within unevaluated contexts. (#GH63845), (#GH67260), (#GH69307),
   (#GH88081), (#GH89496), (#GH90669) and (#GH91633).
 - Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
+- Fix an assertion failure caused by non-lvalue usage in lvalue context. (GH95366).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 7178f081d9cf3..08bee806f172f 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9325,6 +9325,9 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
       Result.IsNullPtr = false;
       return true;
     } else {
+      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..99f83c3e52057
--- /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 ('long' and 'int (*)(void)')}}

>From edc0f2e55883bcd47e6d6aee73730fe0bd8014c0 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 14 Jun 2024 08:50:03 +0300
Subject: [PATCH 2/4] update test expectations

---
 clang/test/Sema/integral-to-ptr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/integral-to-ptr.c b/clang/test/Sema/integral-to-ptr.c
index 99f83c3e52057..b8ab4cb79820d 100644
--- a/clang/test/Sema/integral-to-ptr.c
+++ b/clang/test/Sema/integral-to-ptr.c
@@ -1,3 +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 ('long' and 'int (*)(void)')}}
+int x(void) { e: b: ; return &&e - &&b < x; } // expected-warning {{ordered comparison between pointer and integer}}

>From e40d4c5da45b25775b55ebf9819992f3510b22dd Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 14 Jun 2024 18:16:34 +0300
Subject: [PATCH 3/4] add detailed comment

---
 clang/lib/AST/ExprConstant.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 08bee806f172f..712c3062eb9ac 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9325,6 +9325,10 @@ 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 difference 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;
 

>From 4b525ec660c07637edd843bc921c6bac77762d99 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 14 Jun 2024 18:16:58 +0300
Subject: [PATCH 4/4] update changelog message

---
 clang/docs/ReleaseNotes.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 755557906360b..efd17d8f9a089 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -847,7 +847,8 @@ Bug Fixes to C++ Support
 - Fixed several bugs in capturing variables within unevaluated contexts. (#GH63845), (#GH67260), (#GH69307),
   (#GH88081), (#GH89496), (#GH90669) and (#GH91633).
 - Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
-- Fix an assertion failure caused by non-lvalue usage in lvalue context. (GH95366).
+- Fixed a failed assertion when attempting to convert an integer representing the difference
+  between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (GH95366).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list