Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Clean up loop instruction handling
Browse files Browse the repository at this point in the history
Summary:
The current loop bounds checks are a little unclear and cover an
unreachable case where the number of iterations is both below
the minimum and greater than or equal to the max.

Reviewed By: avp

Differential Revision: D26379687

fbshipit-source-id: b01862ee1bfa10c898e73081810619e2a1c6be73
  • Loading branch information
neildhar authored and facebook-github-bot committed Feb 11, 2021
1 parent 7331644 commit 6fd712c
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/Regex/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,11 +1344,9 @@ auto Context<Traits>::match(State<Traits> *s, bool onlyAtStart)
auto &loopData = s->getLoop(loop->loopId);
uint32_t iteration = loopData.iterations;

uint32_t loopTakenIp = s->ip_ + sizeof(BeginLoopInsn);
uint32_t loopNotTakenIp = loop->notTakenTarget;
const uint32_t loopTakenIp = s->ip_ + sizeof(BeginLoopInsn);

bool doLoopBody = iteration < loop->max;
bool doNotTaken = iteration >= loop->min;
assert(loop->min <= loop->max && "Inconsistent loop bounds");

// Check to see if we have looped more than the minimum number of
// iterations, and if so, whether the subexpression we looped over
Expand All @@ -1360,18 +1358,16 @@ auto Context<Traits>::match(State<Traits> *s, bool onlyAtStart)
loopData.entryPosition == c.offsetFromLeft())
BACKTRACK();

if (!doLoopBody && !doNotTaken) {
BACKTRACK();
} else if (doLoopBody && !doNotTaken) {
if (iteration < loop->min) {
if (!prepareToEnterLoopBody(s, loop, backtrackStack))
return nullptr;
s->ip_ = loopTakenIp;
} else if (doNotTaken && !doLoopBody) {
} else if (iteration == loop->max) {
s->ip_ = loop->notTakenTarget;
} else {
assert(
doNotTaken && doLoopBody &&
"Must be exploring loop not taken and body");
// We are within the target iteration range, figure out whether we
// should continue or exit.
assert(iteration >= loop->min && iteration < loop->max);
if (!loop->greedy) {
// Backtrack by entering this non-greedy loop.
loopData.entryPosition = c.offsetFromLeft();
Expand All @@ -1387,7 +1383,7 @@ auto Context<Traits>::match(State<Traits> *s, bool onlyAtStart)
if (!pushBacktrack(
backtrackStack,
BacktrackInsn::makeSetPosition(
loopNotTakenIp, c.currentPointer()))) {
loop->notTakenTarget, c.currentPointer()))) {
error_ = MatchRuntimeErrorType::MaxStackDepth;
return nullptr;
}
Expand Down

0 comments on commit 6fd712c

Please sign in to comment.