Skip to content

Commit 8618c86

Browse files
committed
[clang][Interp] Check for dereferencability before doing ltor cast
1 parent fb2b5cd commit 8618c86

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/AST/Interp/EvalEmitter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
152152

153153
// Implicitly convert lvalue to rvalue, if requested.
154154
if (ConvertResultToRValue) {
155+
if (!Ptr.isDereferencable())
156+
return false;
155157
// Never allow reading from a non-const pointer, unless the memory
156158
// has been created in this evaluation.
157159
if (!Ptr.isConst() && Ptr.block()->getEvalID() != Ctx.getEvalID())

clang/test/AST/Interp/c23.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ constexpr _Bool inf2 = (1.0/0.0 == __builtin_inf()); // both-error {{must be ini
88
// both-note {{division by zero}}
99
constexpr _Bool inf3 = __builtin_inf() == __builtin_inf();
1010

11+
/// Used to crash.
12+
struct S {
13+
int x;
14+
char c;
15+
float f;
16+
};
1117

18+
#define DECL_BUFFER(Ty, Name) alignas(Ty) unsigned char Name[sizeof(Ty)]
19+
20+
char bar() {
21+
DECL_BUFFER(struct S, buffer);
22+
((struct S *)buffer)->c = 'a';
23+
return ((struct S *)buffer)->c;
24+
}

0 commit comments

Comments
 (0)