[ 
https://issues.apache.org/jira/browse/CLI-193?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12878422#action_12878422
 ] 

Travis McLeskey commented on CLI-193:
-------------------------------------

You should be able to get it to throw with a call like this:

  findWrapPos( "hello", 3, 0 );  // should return -1

The exception gets thrown whenever the passed-in string does not contain any 
spaces, tabs, or newlines. It looks like line 914 was also expecting line 908 
to have a <.


Also, I think there are other bugs. In this call:

  findWrapPos( "helloooo\noo\ngoodbye", 6, 7 );  // should return 8

we want it to short circuit and return the first newline if there is one in the 
substring starting at 7 with width 6 ("o\noo\ng"), and so it should return the 
index of the first '\n', which is 8. However, on lines 876 and 877, it's doing 
"pos <= width", instead of "pos <= startPos+width", so it won't return, and it 
will end up (incorrectly, I think) returning the index of the second newline's 
position. And once you fix lines 876 and 877, it's going to return 9 instead of 
8, which I think is incorrect. The other return statements return pos, not 
pos+1, and I think line 879 should do the same.

> StringIndexOutOfBoundsException in HelpFormatter.findWrapPos
> ------------------------------------------------------------
>
>                 Key: CLI-193
>                 URL: https://issues.apache.org/jira/browse/CLI-193
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-1.x
>    Affects Versions: 1.2
>            Reporter: Travis McLeskey
>            Priority: Critical
>             Fix For: 1.3
>
>         Attachments: cli-helpformatter-stringIndexOOB.patch
>
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> In the last while loop in HelpFormatter.findWrapPos, it can pass 
> text.length() to text.charAt(int), which throws a 
> StringIndexOutOfBoundsException. The first expression in that while loop 
> condition should use a <, not a <=.
> This is on line 908 in r779646:
>   
> http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java?revision=779646&view=markup

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to