Skip to content

Commit 2ebe479

Browse files
authored
[Clang] Disallow non-lvalue values in constant expressions to prevent invalid pointer offset computation (#95479)
Fixes #95366
1 parent a1994ae commit 2ebe479

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,8 @@ Bug Fixes to C++ Support
859859
(#GH88081), (#GH89496), (#GH90669) and (#GH91633).
860860
- Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
861861
- Clang now instantiates local constexpr functions eagerly for constant evaluators. (#GH35052), (#GH94849)
862+
- Fixed a failed assertion when attempting to convert an integer representing the difference
863+
between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (#GH95366).
862864

863865
Bug Fixes to AST Handling
864866
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/ExprConstant.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9325,6 +9325,13 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
93259325
Result.IsNullPtr = false;
93269326
return true;
93279327
} else {
9328+
// In rare instances, the value isn't an lvalue.
9329+
// For example, when the value is the difference between the addresses of
9330+
// two labels. We reject that as a constant expression because we can't
9331+
// compute a valid offset to convert into a pointer.
9332+
if (!Value.isLValue())
9333+
return false;
9334+
93289335
// Cast is of an lvalue, no need to change value.
93299336
Result.setFrom(Info.Ctx, Value);
93309337
return true;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: %clang_cc1 %s -verify -fsyntax-only -std=c11
2+
3+
int x(void) { e: b: ; return &&e - &&b < x; } // expected-warning {{ordered comparison between pointer and integer}}

0 commit comments

Comments
 (0)