[clang] [clang][bytecode] Check __builtin_memcpy for null pointers (PR #118313)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 2 07:52:45 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/118313.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+8)
- (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+8)
``````````diff
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}}
+
+
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/118313
More information about the cfe-commits
mailing list