https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/118313
None >From 0cc2c1a52538a8fa5fe89bba77e83e00942eeaf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com> Date: Mon, 2 Dec 2024 16:47:50 +0100 Subject: [PATCH] [clang][bytecode] Check __builtin_memcpy for null pointers --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 8 ++++++++ clang/test/AST/ByteCode/builtin-functions.cpp | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index a217578e4936b4..de9f1f09e46405 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -1761,6 +1761,14 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC, return true; } + if (SrcPtr.isZero() || DestPtr.isZero()) { + Pointer DiagPtr = (SrcPtr.isZero() ? SrcPtr : DestPtr); + S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null) + << /*IsMove=*/false << /*IsWchar=*/false << !SrcPtr.isZero() + << SrcPtr.toDiagnosticString(S.getASTContext()); + return false; + } + if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr)) return false; diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp index 61b78e8928df4c..d38a8a7f413c52 100644 --- a/clang/test/AST/ByteCode/builtin-functions.cpp +++ b/clang/test/AST/ByteCode/builtin-functions.cpp @@ -1116,4 +1116,12 @@ namespace BuiltinMemcpy { return b; } static_assert(simple() == 12); + + + extern struct Incomplete incomplete; + constexpr struct Incomplete *null_incomplete = 0; + static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \ + // both-note {{source of 'memcpy' is nullptr}} + + } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits