Skip to content

Commit 5a8c4b5

Browse files
committed
[clang][Interp] Don't allow reading from non-const blocks when returning
1 parent 40f4bd1 commit 5a8c4b5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clang/lib/AST/Interp/EvalEmitter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
151151

152152
// Implicitly convert lvalue to rvalue, if requested.
153153
if (ConvertResultToRValue) {
154+
// Never allow reading from a non-const pointer, unless the memory
155+
// has been created in this evaluation.
156+
if (!Ptr.isConst() && Ptr.block()->getEvalID() != Ctx.getEvalID())
157+
return false;
158+
154159
if (std::optional<APValue> V = Ptr.toRValue(Ctx)) {
155160
EvalResult.setValue(*V);
156161
} else {

clang/test/AST/Interp/const-temporaries.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ struct R { mutable long x; };
9090
struct Z2 { const R &x, y; };
9191
Z2 z2 = { R{1}, z2.x.x = 10 };
9292

93+
// CHECK: __cxa_atexit({{.*}} @_ZN1BD1Ev, {{.*}} @b
94+
95+
// CHECK: define
96+
// CHECK-NOT: @_ZGRN21ModifyStaticTemporary1cE_
97+
// CHECK: store {{.*}} @_ZGRN21ModifyStaticTemporary1cE_, {{.*}} @_ZN21ModifyStaticTemporary1cE
98+
// CHECK: add
99+
// CHECK: store
100+
// CHECK: load {{.*}} @_ZN21ModifyStaticTemporary1bE
101+
// CHECK: store {{.*}} @_ZN21ModifyStaticTemporary1cE

0 commit comments

Comments
 (0)