You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "garydgregory (via GitHub)" <gi...@apache.org> on 2026/05/17 14:18:42 UTC
[PR] WordUtils.wrap() zero-width wrapOn regex match causes infinite loop [commons-lang]
garydgregory opened a new pull request, #1649:
URL: https://github.com/apache/commons-lang/pull/1649
Calling the deprecated method `WordUtils.wrap(String, int, String, boolean, String)` with zero-width wrapOn regex match causes infinite loop
- [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [x] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI).
- [x] OP used AI to create the original report.
- [x] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself.
- [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] WordUtils.wrap() zero-width wrapOn regex match causes infinite loop [commons-lang]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #1649:
URL: https://github.com/apache/commons-lang/pull/1649#discussion_r3255263385
##########
src/main/java/org/apache/commons/lang3/text/WordUtils.java:
##########
@@ -652,21 +652,19 @@ public static String wrap(final String str, int wrapLength, String newLineStr, f
str.substring(offset, Math.min((int) Math.min(Integer.MAX_VALUE, offset + wrapLength + 1L), inputLineLength)));
if (matcher.find()) {
if (matcher.start() == 0) {
- offset += matcher.end();
+ // If the match is zero-width, advance by at least 1 to avoid infinite loop.
+ offset += matcher.end() > 0 ? matcher.end() : 1;
Review Comment:
This stops the infinite loop, but drops the first character of the line.
Fixing this would require fixing a couple of other lines that assume matches are always of length 1. I am not sure if it is worth fixing in a **deprecated** method, but we should add some tests for variable-length matches in Commons Text.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] WordUtils.wrap() zero-width wrapOn regex match causes infinite loop [commons-lang]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #1649:
URL: https://github.com/apache/commons-lang/pull/1649#discussion_r3255402556
##########
src/main/java/org/apache/commons/lang3/text/WordUtils.java:
##########
@@ -652,21 +652,19 @@ public static String wrap(final String str, int wrapLength, String newLineStr, f
str.substring(offset, Math.min((int) Math.min(Integer.MAX_VALUE, offset + wrapLength + 1L), inputLineLength)));
if (matcher.find()) {
if (matcher.start() == 0) {
- offset += matcher.end();
+ // If the match is zero-width, advance by at least 1 to avoid infinite loop.
+ offset += matcher.end() > 0 ? matcher.end() : 1;
Review Comment:
I'll make a PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] WordUtils.wrap() zero-width wrapOn regex match causes infinite loop [commons-lang]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #1649:
URL: https://github.com/apache/commons-lang/pull/1649#discussion_r3255284366
##########
src/main/java/org/apache/commons/lang3/text/WordUtils.java:
##########
@@ -652,21 +652,19 @@ public static String wrap(final String str, int wrapLength, String newLineStr, f
str.substring(offset, Math.min((int) Math.min(Integer.MAX_VALUE, offset + wrapLength + 1L), inputLineLength)));
if (matcher.find()) {
if (matcher.start() == 0) {
- offset += matcher.end();
+ // If the match is zero-width, advance by at least 1 to avoid infinite loop.
+ offset += matcher.end() > 0 ? matcher.end() : 1;
Review Comment:
Hello @ppkarwasz
Good find! Do you have a unit test we can add for that and fix?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] WordUtils.wrap() zero-width wrapOn regex match causes infinite loop [commons-lang]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #1649:
URL: https://github.com/apache/commons-lang/pull/1649#discussion_r3255543245
##########
src/main/java/org/apache/commons/lang3/text/WordUtils.java:
##########
@@ -652,21 +652,19 @@ public static String wrap(final String str, int wrapLength, String newLineStr, f
str.substring(offset, Math.min((int) Math.min(Integer.MAX_VALUE, offset + wrapLength + 1L), inputLineLength)));
if (matcher.find()) {
if (matcher.start() == 0) {
- offset += matcher.end();
+ // If the match is zero-width, advance by at least 1 to avoid infinite loop.
+ offset += matcher.end() > 0 ? matcher.end() : 1;
Review Comment:
#1655
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] WordUtils.wrap() zero-width wrapOn regex match causes infinite loop [commons-lang]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #1649:
URL: https://github.com/apache/commons-lang/pull/1649
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org