You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (Issue Comment Edited) (JIRA)" <ji...@apache.org> on 2012/04/04 15:39:25 UTC

[jira] [Issue Comment Edited] (LANG-686) StringUtils.replaceEachRepeatedly("aaa", new String[]{"aa"}, new String[]{"aXa"}); throw an exception

    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13245710#comment-13245710 ] 

Thomas Neidhart edited comment on LANG-686 at 4/4/12 1:38 PM:
--------------------------------------------------------------

I have worked on this a bit and provide a patch that does the following:

 * refactor the replaceEach method to avoid code duplication
 * change loop detection code

Instead of using a timeToLive variable that tries to detect infinite loops, I used a quite simple but effective method:

Whenever at the end of a replace cycle we end up having a result string that has already be seen so far, we have possibly detected an infinite loop and thus abort the execution (with an exception as before).

I have also added two additional unit test cases.

Edit:

The new loop detection code does not prevent stackoverflow exceptions of course. But as others have pointed out, just let it happen (it is anyway very unlikely to occur, need to come up with a test case for it somehow). I would prefer correct behavior in normal cases, and a robust loop detection for recursive/weird cases.
                
      was (Author: tn):
    I have worked on this a bit and provide a patch that does the following:

 * refactor the replaceEach method to avoid 
   code duplication
 * change loop detection code

Instead using a timeToLive variable that trys to detect infinite loops, I use a quite simple but effective method:

Whenever at the end of a replace cycle we end up having a result string that has already be seen so far, we have possibly detected an infinite loop and thus abort the execution (with an exception as before).

I have also added two additional unit test cases.
                  
> StringUtils.replaceEachRepeatedly("aaa", new String[]{"aa"}, new String[]{"aXa"}); throw an exception
> -----------------------------------------------------------------------------------------------------
>
>                 Key: LANG-686
>                 URL: https://issues.apache.org/jira/browse/LANG-686
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 2.6
>         Environment: jdk 1.6.24, windows xp pro sp3, eclipse helios
>            Reporter: qed
>             Fix For: 3.x
>
>         Attachments: LANG-686.patch
>
>
> After executing line
> StringUtils.replaceEachRepeatedly("aaa", new String[]{"aa"}, new String[]{"aXa"});
> exception is thrown:
> Exception in thread "main" java.lang.IllegalStateException: TimeToLive of -1 is less than 0: aXaXa
> 	at org.apache.commons.lang.StringUtils.replaceEach(StringUtils.java:3986)
> 	at org.apache.commons.lang.StringUtils.replaceEach(StringUtils.java:4099)
> 	at org.apache.commons.lang.StringUtils.replaceEach(StringUtils.java:4099)
> 	at org.apache.commons.lang.StringUtils.replaceEachRepeatedly(StringUtils.java:3920)

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira