Skip to content

Commit cfa07cc

Browse files
authored
[clang][bytecode] Fix builtin_memchr with non-0 start index (#131633)
1 parent ca1bde0 commit cfa07cc

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,8 +2045,10 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
20452045
(ID == Builtin::BIstrchr || ID == Builtin::BI__builtin_strchr);
20462046

20472047
size_t Index = Ptr.getIndex();
2048+
size_t Step = 0;
20482049
for (;;) {
2049-
const Pointer &ElemPtr = Index > 0 ? Ptr.atIndex(Index) : Ptr;
2050+
const Pointer &ElemPtr =
2051+
(Index + Step) > 0 ? Ptr.atIndex(Index + Step) : Ptr;
20502052

20512053
if (!CheckLoad(S, OpPC, ElemPtr))
20522054
return false;
@@ -2060,8 +2062,8 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
20602062
if (StopAtZero && V == 0)
20612063
break;
20622064

2063-
++Index;
2064-
if (MaxLength && Index == MaxLength->getZExtValue())
2065+
++Step;
2066+
if (MaxLength && Step == MaxLength->getZExtValue())
20652067
break;
20662068
}
20672069

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,11 @@ namespace Memchr {
14591459
constexpr bool b = !memchr("hello", 'h', 3); // both-error {{constant expression}} \
14601460
// both-note {{non-constexpr function 'memchr' cannot be used in a constant expression}}
14611461

1462+
constexpr bool f() {
1463+
const char *c = "abcdef";
1464+
return __builtin_char_memchr(c + 1, 'f', 1) == nullptr;
1465+
}
1466+
static_assert(f());
14621467
}
14631468

14641469
namespace Strchr {

0 commit comments

Comments
 (0)