Skip to content

Conversation

@ParanoidUser
Copy link
Contributor

https://issues.apache.org/jira/browse/LANG-1753

The method replaceEachRepeatedly calls replaceEach repeatedly until no more replacements are possible or timeToLive < 0. The timeToLive starts with the value of searchList.length. The current implementation can't distinguish between infinite loops and deeply nested string patterns. In both cases, it continues replacements until it hits the time-to-live limit and throws IllegalStateException if replacements are still possible.

The problem with LANG-1528 fix is that in some cases, it allows partially replaced strings after TTL is exceeded. This fix reverts replaceEach logic back to version 3.10 (see PR #505), but allows deeper dives due to an increased initial TTL value. The initial TTL value is now set to the greater of searchList.length or up to 5 levels deep. This default value was chosen based on rational considerations.

*/
public static String replaceEachRepeatedly(final String text, final String[] searchList, final String[] replacementList) {
return replaceEach(text, searchList, replacementList, true, ArrayUtils.getLength(searchList));
int timeToLive = Math.max(ArrayUtils.getLength(searchList), DEFAULT_TTL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Javadoc only mentions endless loops ("if the search is repeating and there is an endless loop"). It would be clearer if it also included a mention of the TTL to avoid confusion.

Comment on lines +1999 to +2000
assertEquals("a", StringUtils.replaceEachRepeatedly("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
new String[]{"aa"}, new String[]{"a"}));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding an example of IllegalStateException with a shorter input, like:

assertThrows(
	IllegalStateException.class,
	() -> StringUtils.replaceEachRepeatedly("aaaaaab", new String[] {"ab"}, new String[] {"b"}),
	"Should be a circular reference");

It might surprise newcomers that even far fewer than 32 repetitions can trigger an IllegalStateException.

@palacsint
Copy link

Thanks for the fix! Seems perfect for us.

@garydgregory garydgregory merged commit 972aa7b into apache:master Oct 15, 2024
LuciferYang pushed a commit to apache/spark that referenced this pull request Jul 12, 2025
### What changes were proposed in this pull request?

This PR aims to upgrade `commons-lang3` to 3.18.0.

Although SPARK-52469 removes ` org.apache.commons.lang3.JavaVersion` usages, we still use many methods of `commons-lang3`.
- #51174

### Why are the changes needed?

To bring the latest bug fixes.
- https://commons.apache.org/proper/commons-lang/changes.html#a3.18.0
  - apache/commons-lang/pull/1327
  - apache/commons-lang/pull/1297
  - apache/commons-lang/pull/1273

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass the CIs.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #51460 from dongjoon-hyun/SPARK-52778.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
a0x8o added a commit to a0x8o/spark that referenced this pull request Jul 13, 2025
### What changes were proposed in this pull request?

This PR aims to upgrade `commons-lang3` to 3.18.0.

Although SPARK-52469 removes ` org.apache.commons.lang3.JavaVersion` usages, we still use many methods of `commons-lang3`.
- #51174

### Why are the changes needed?

To bring the latest bug fixes.
- https://commons.apache.org/proper/commons-lang/changes.html#a3.18.0
  - apache/commons-lang/pull/1327
  - apache/commons-lang/pull/1297
  - apache/commons-lang/pull/1273

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass the CIs.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #51460 from dongjoon-hyun/SPARK-52778.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants