Permalink
Browse files

Endless loop in JOrphanUtils#replaceAllWithRegex when regex is contai…

…ned in replacement

Bugzilla Id: 61054


git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1793191 13f79535-47bb-0310-9956-ffa450edef68
1 parent d8f6485 commit e0e372b09f41395a8a2982b74bf1aad856d53b68 @FSchumacher FSchumacher committed Apr 29, 2017
@@ -632,23 +632,18 @@ public static void canSafelyWriteToFolder(File folder)
java.util.regex.Pattern pattern = caseSensitive ?
java.util.regex.Pattern.compile(regex) :
java.util.regex.Pattern.compile(regex, java.util.regex.Pattern.CASE_INSENSITIVE);
- String newText = source;
final String replacementQuoted = Matcher.quoteReplacement(replacement);
- Matcher matcher = pattern.matcher(newText);
+ Matcher matcher = pattern.matcher(source);
int totalReplaced = 0;
- while(true) {
- String previousText = newText;
- newText = matcher.replaceFirst(replacementQuoted);
- matcher = pattern.matcher(newText);
- if(newText.equals(previousText)) {
- break;
- } else {
- totalReplaced++;
- }
+ StringBuffer result = new StringBuffer(); // NOSONAR Matcher#appendReplacement needs a StringBuffer
+ while(matcher.find()) {
+ matcher.appendReplacement(result, replacementQuoted);
+ totalReplaced++;
}
+ matcher.appendTail(result);
return new Object[]{
- newText,
+ result.toString(),
totalReplaced
};
}
@@ -298,6 +298,13 @@ public void testRightAlign() {
}
@Test
+ public void testReplaceAllWithRegexWithSearchValueContainedInReplaceValue() {
+ // Bug 61054
+ Assert.assertArrayEquals(new Object[] { "abcd", 1 },
+ JOrphanUtils.replaceAllWithRegex("abc", "abc", "abcd", true));
+ }
+
+ @Test
public void testReplaceAllWithRegex() {
Assert.assertArrayEquals(new Object[] {"toto", 0},
JOrphanUtils.replaceAllWithRegex("toto","ti", "ta", true));
View
@@ -162,12 +162,13 @@ Summary
<h3>Report / Dashboard</h3>
<ul>
</ul>
-
+
<h3>General</h3>
<ul>
<li><bug>60994</bug>Fix some typo in comments or log messages. <pr>289</pr> and <pr>290</pr></li>
<li><bug>61011</bug>Replace occurrences count is not correct (Path and Host replacement are counted twice)</li>
<li><bug>61026</bug>Cannot run program "keytool": CreateProcess error=2 when starting JMeter 3.2 in gui mode</li>
+ <li><bug>61054</bug>Endless loop in JOrphanUtils#replaceAllWithRegex when regex is contained in replacement</li>
</ul>
<!-- =================== Thanks =================== -->

0 comments on commit e0e372b

Please sign in to comment.