[clang] [clang][bytecode] Check builtin_memchr() for one-past-end pointers (PR #174187)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 2 00:32:35 PST 2026
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/174187 at github.com>
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
We can't read from them and this fails later.
Fixes https://github.com/llvm/llvm-project/issues/173942
---
Full diff: https://github.com/llvm/llvm-project/pull/174187.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+18-2)
- (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+4)
- (modified) clang/test/AST/ByteCode/builtins.c (+1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 57d5f0ae6eed3..065870c5c0ab5 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -64,6 +64,20 @@ static APSInt popToAPSInt(InterpState &S, QualType T) {
return popToAPSInt(S.Stk, *S.getContext().classify(T));
}
+/// Check for common reasons a pointer can't be read from, which
+/// are usually not diagnosed in a builtin function.
+static bool isReadable(const Pointer &P) {
+ if (P.isDummy())
+ return false;
+ if (!P.isBlockPointer())
+ return false;
+ if (!P.isLive())
+ return false;
+ if (P.isOnePastEnd())
+ return false;
+ return true;
+}
+
/// Pushes \p Val on the stack as the type given by \p QT.
static void pushInteger(InterpState &S, const APSInt &Val, QualType QT) {
assert(QT->isSignedIntegerOrEnumerationType() ||
@@ -1794,8 +1808,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
return false;
}
- // Can't read from dummy pointers.
- if (DestPtr.isDummy() || SrcPtr.isDummy())
+ if (!isReadable(DestPtr) || !isReadable(SrcPtr))
return false;
if (DestPtr.getType()->isIncompleteType()) {
@@ -2078,6 +2091,9 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
return false;
}
+ if (!isReadable(Ptr))
+ return false;
+
if (ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr) {
int64_t DesiredTrunc;
if (S.getASTContext().CharTy->isSignedIntegerType())
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 3076b5239ebbe..3cde5a2b42e3d 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1649,6 +1649,10 @@ namespace Memchr {
return __builtin_char_memchr(c + 1, 'f', 1) == nullptr;
}
static_assert(f());
+
+
+ extern const char char_memchr_arg[0l];
+ char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32);
}
namespace Strchr {
diff --git a/clang/test/AST/ByteCode/builtins.c b/clang/test/AST/ByteCode/builtins.c
index a51260cd3431f..5be5455ab8813 100644
--- a/clang/test/AST/ByteCode/builtins.c
+++ b/clang/test/AST/ByteCode/builtins.c
@@ -17,3 +17,4 @@ int structStrlen(void) {
return 1;
}
+void f() { __builtin_memcpy(f, f, 1); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/174187
More information about the cfe-commits
mailing list