Skip to content

Commit 0cc2c1a

Browse files
committed
[clang][bytecode] Check __builtin_memcpy for null pointers
1 parent b587b91 commit 0cc2c1a

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
@@ -1761,6 +1761,14 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
17611761
return true;
17621762
}
17631763

1764+
if (SrcPtr.isZero() || DestPtr.isZero()) {
1765+
Pointer DiagPtr = (SrcPtr.isZero() ? SrcPtr : DestPtr);
1766+
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
1767+
<< /*IsMove=*/false << /*IsWchar=*/false << !SrcPtr.isZero()
1768+
<< SrcPtr.toDiagnosticString(S.getASTContext());
1769+
return false;
1770+
}
1771+
17641772
if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
17651773
return false;
17661774

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,4 +1116,12 @@ namespace BuiltinMemcpy {
11161116
return b;
11171117
}
11181118
static_assert(simple() == 12);
1119+
1120+
1121+
extern struct Incomplete incomplete;
1122+
constexpr struct Incomplete *null_incomplete = 0;
1123+
static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1124+
// both-note {{source of 'memcpy' is nullptr}}
1125+
1126+
11191127
}

0 commit comments

Comments
 (0)