[clang] fc9052e - [clang][bytecode] Check __builtin_memcpy for null pointers (#118313)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 2 18:37:00 PST 2024
Author: Timm Baeder
Date: 2024-12-03T03:36:57+01:00
New Revision: fc9052ee258e35c5aaba3dc2c1419870975f3a7a
URL: https://github.com/llvm/llvm-project/commit/fc9052ee258e35c5aaba3dc2c1419870975f3a7a
DIFF: https://github.com/llvm/llvm-project/commit/fc9052ee258e35c5aaba3dc2c1419870975f3a7a.diff
LOG: [clang][bytecode] Check __builtin_memcpy for null pointers (#118313)
Added:
Modified:
clang/lib/AST/ByteCode/InterpBuiltin.cpp
clang/test/AST/ByteCode/builtin-functions.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index db3703a60db699..d7e32c491b03bc 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1796,6 +1796,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()
+ << DiagPtr.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 9a6a31b5ec4c06..dfee35d6399a6c 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1138,4 +1138,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}}
+
+
}
More information about the cfe-commits
mailing list