Skip to content

Commit fc9052e

Browse files
authored
[clang][bytecode] Check __builtin_memcpy for null pointers (#118313)
1 parent 9597971 commit fc9052e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,14 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
17961796
return true;
17971797
}
17981798

1799+
if (SrcPtr.isZero() || DestPtr.isZero()) {
1800+
Pointer DiagPtr = (SrcPtr.isZero() ? SrcPtr : DestPtr);
1801+
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
1802+
<< /*IsMove=*/false << /*IsWchar=*/false << !SrcPtr.isZero()
1803+
<< DiagPtr.toDiagnosticString(S.getASTContext());
1804+
return false;
1805+
}
1806+
17991807
if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
18001808
return false;
18011809

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,4 +1138,12 @@ namespace BuiltinMemcpy {
11381138
return b;
11391139
}
11401140
static_assert(simple() == 12);
1141+
1142+
1143+
extern struct Incomplete incomplete;
1144+
constexpr struct Incomplete *null_incomplete = 0;
1145+
static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1146+
// both-note {{source of 'memcpy' is nullptr}}
1147+
1148+
11411149
}

0 commit comments

Comments
 (0)