Skip to content

Commit

Permalink
Merge pull request #4525 from RaiKoHoff/Dev_Master
Browse files Browse the repository at this point in the history
+fix: infinite loop using SCI_REPLACETARGETMINIMAL in replace all loop in range
  • Loading branch information
hpwamr authored Feb 3, 2023
2 parents 8db1f8b + ac3b6f7 commit 79f0995
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -7382,6 +7382,10 @@ bool EditReplace(HWND hwnd, LPEDITFINDREPLACE lpefr)
// EditReplaceAllInRange()
//
//

// force NOT minimal replacement to calculate next match correctly
#define REPLACE_ALL_MSG(M) ((M) == SCI_REPLACETARGETRE ? SCI_REPLACETARGETRE : SCI_REPLACETARGET)

int EditReplaceAllInRange(HWND hwnd, LPEDITFINDREPLACE lpefr, DocPos iStartPos, DocPos iEndPos, DocPos *enlargement)
{
if (iStartPos > iEndPos) {
Expand All @@ -7400,13 +7404,11 @@ int EditReplaceAllInRange(HWND hwnd, LPEDITFINDREPLACE lpefr, DocPos iStartPos,
bool const bRegexStartOfLine = bIsRegExpr && (wchFind[0] == L'^');

// SCI_REPLACETARGET or SCI_REPLACETARGETRE
int iReplaceMsg = SciCall_GetChangeHistory() ? SCI_REPLACETARGETMINIMAL : SCI_REPLACETARGET;
int iReplaceMsg = SCI_REPLACETARGET;
char* const pszReplace = _GetReplaceString(hwnd, lpefr, &iReplaceMsg);
if (!pszReplace) {
return -1; // recoding of clipboard canceled
}
bool const bMinRepl = (SCI_REPLACETARGETMINIMAL == iReplaceMsg);
DocPos const pszReplaceLen = strlen(pszReplace);

DocPos const _saveTargetBeg_ = SciCall_GetTargetStart();
DocPos const _saveTargetEnd_ = SciCall_GetTargetEnd();
Expand All @@ -7426,14 +7428,14 @@ int EditReplaceAllInRange(HWND hwnd, LPEDITFINDREPLACE lpefr, DocPos iStartPos,
while ((iPos >= 0LL) && (start <= iEndPos)) {
DocLn const lnCnt = bRegexStartOfLine ? SciCall_GetLineCount() : 0;
SciCall_SetTargetRange(iPos, end);
DocPos const replLen = Sci_ReplaceTargetEx(iReplaceMsg, -1, pszReplace);
DocPos const replLen = Sci_ReplaceTargetEx(REPLACE_ALL_MSG(iReplaceMsg), -1, pszReplace);
++iCount;
// start next search behind replacement
iStartPos = SciCall_GetTargetEnd();
if (bRegexStartOfLine && (Sci_GetLineStartPosition(iStartPos) == iStartPos) && (SciCall_GetLineCount() == lnCnt)) {
iStartPos = SciCall_PositionAfter(iStartPos);
}
iEndPos += (bMinRepl ? pszReplaceLen : replLen) - (end - iPos);
iEndPos += replLen - (end - iPos);
start = iStartPos;
end = iEndPos;
iPos = (start <= end) ? _FindInTarget(wchFind, sFlags, &start, &end, true, FRMOD_NORM) : -1LL;
Expand Down

0 comments on commit 79f0995

Please sign in to comment.