You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "qed (JIRA)" <ji...@apache.org> on 2011/03/10 14:20:59 UTC

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

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


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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

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

Posted by "Henri Yandell (Updated) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Fix Version/s:     (was: 3.0.2)
                   3.0.3
    
> 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.0.3
>
>
> 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

        

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

Posted by "Henri Yandell (Updated) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Fix Version/s:     (was: 3.2)
                   3.x
    
> 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
>
>
> 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

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13071526#comment-13071526 ] 

Henri Yandell commented on LANG-686:
------------------------------------

Digging into this, the search should have been:

"aaa"
"aXaa"
"aXaXa"

Which is hardly the deepest loop.

The TTL would have been 1, ie) the length of the inputted String[] { "aXa" }.

I wonder if the issue is that TTL needs to be longer (which is what qed did by adding extra ""s).

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Thomas Neidhart (Issue Comment Edited) (JIRA)" <ji...@apache.org>.
    [ 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

        

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

Posted by "qed (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13005226#comment-13005226 ] 

qed commented on LANG-686:
--------------------------

I solved this problem by making this call instead:

StringUtils.replaceEachRepeatedly("aaa", new String[]{"aa", ""}, new String[]{"aXa", ""});

Maybe there should be a limit to number of iterations or something similar. I didn't know that this function is not supposed to support recursion. I think this function is good suited when (java) regex fail in matching already replaced part of string and hence it shouldn't be dicouraged to use this function as a solution imho.

> 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
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Remaining Estimate:     (was: 1h)
     Original Estimate:     (was: 1h)

> 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.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Fix Version/s: 3.1

> 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.1
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Fix Version/s:     (was: 3.0.1)

Moving to 3.0.2. Note that the error message was improved and javadoc Exceptions fixed in 3.0.1. When resolving, make 3.0.1 a fix version as well.

> 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.0.2
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "qed (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13071774#comment-13071774 ] 

qed commented on LANG-686:
--------------------------

> output of one loop is the input of another
And that's a problem? That's how grammars are working. I don't think that this function should prevent such behaviour (ie. output of one loop should be allowed to be input of another loop).

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13071533#comment-13071533 ] 

Henri Yandell commented on LANG-686:
------------------------------------

For this regexp; and I suspect it's the worst, yet vaguely sane, case; the TTL would have needed to have been text.length() - 1. For large text that's going to offer no protection against StackOverflowError. Options would seem to be:

1) Leave as is, improve IllegalStateException message (and update in javadoc which says IllegalArgumentException).
2) Stop protecting against StackOverflowError.
3) Increase the size by a marginal, yet arbitrary, amount.

I'm going to continue to go with #1.

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13071603#comment-13071603 ] 

Sebb commented on LANG-686:
---------------------------

The message is ambiguous; protecting could refer to the first part of the sentence rather than the exception itself.

How about

"Aborting to protect against StackOverflowError - output of one loop is the input of another"

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13072569#comment-13072569 ] 

Henri Yandell commented on LANG-686:
------------------------------------

Sebb: I've changed to your suggested text (r1152021).

qed: Not disagreeing. For 3.0.1, improving the error message to avoid your 'WTF???' is an improvement. Whether we simply strip out the TTL code and let the StackOverflowError happen is another question. My personal feeling is in line with yours (I think). That is - "let it happen".

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Fix Version/s:     (was: 3.x)
                   3.0.1

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13071554#comment-13071554 ] 

Henri Yandell commented on LANG-686:
------------------------------------

Improved error message added (I hope):

"Output of one loop is the input of another; protecting from potential StackOverflowError"

I think there is value in #3 above as well. Some arbitrarily small number to be a minimum TTL. 

> 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.0.1
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Thomas Neidhart (Updated) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Neidhart updated LANG-686:
---------------------------------

    Attachment: LANG-686.patch

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

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-686:
-------------------------------

    Fix Version/s: 3.0.2

> 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.0.1, 3.0.2
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13007341#comment-13007341 ] 

Henri Yandell commented on LANG-686:
------------------------------------

At the very least the exception could explain what the problem is rather than what the state of an obscure private variable is.

> 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.1
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

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

Posted by "Maurizio Cucchiara (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13005142#comment-13005142 ] 

Maurizio Cucchiara commented on LANG-686:
-----------------------------------------

I'm not sure this is a bug. The IllegalStateException was throwned because your substitution pattern triggers a (though little) recursion, it's a kind of prevention, in order to avoid StackOverflowError

> 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
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira