Skip to content

Commit db9549c

Browse files
authored
[clang][bytecode] Check builtin_memchr() for one-past-end pointers (#174187)
We can't read from them and this fails later. Fixes llvm/llvm-project#173942
1 parent 0394ad1 commit db9549c

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ static bool isReadable(const Pointer &P) {
7373
return false;
7474
if (!P.isLive())
7575
return false;
76+
if (P.isOnePastEnd())
77+
return false;
7678
return true;
7779
}
7880

@@ -2089,6 +2091,9 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
20892091
return false;
20902092
}
20912093

2094+
if (!isReadable(Ptr))
2095+
return false;
2096+
20922097
if (ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr) {
20932098
int64_t DesiredTrunc;
20942099
if (S.getASTContext().CharTy->isSignedIntegerType())

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,10 @@ namespace Memchr {
16491649
return __builtin_char_memchr(c + 1, 'f', 1) == nullptr;
16501650
}
16511651
static_assert(f());
1652+
1653+
1654+
extern const char char_memchr_arg[0l];
1655+
char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32);
16521656
}
16531657

16541658
namespace Strchr {

0 commit comments

Comments
 (0)