On Fri, 7 Oct 2022 09:00:32 GMT, Prasanta Sadhukhan <psadhuk...@openjdk.org> 
wrote:

>> javax.swing.text.AbstractDocument$BranchElement.replace(...) method throws 
>> an `ArrayIndexOutOfBoundsException: arraycopy: length -1 is negative` when 
>> using an UndoManager on the default document of a JTextArea and you try to 
>> undo the insertion of a LEFT-TO-RIGHT language (e.g. Arabic) that is 
>> immediately followed by setting the component orientation on the JTextArea.
>> 
>> This is because System.arrayCopy() is called with -ve length because of the 
>> calculation done in AbstractDocment.replace where `src` is of 2bytes because 
>> of unicode text causing `nmove` to become -ve if `nchildren` is 1 (an 
>> unicode character is inserted)
>> 
>> System.arrayCopy throws `IndexOutOfBoundsException if:
>> 
>>     The srcPos argument is negative.
>>     The destPos argument is negative.
>>     The length argument is negative
>> 
>> 
>> so the fix is made to make  nmove, src, dest +ve
>> Also, Element.getElement() can return null which is not checked, causing 
>> NPE, if deletion of text is done which results in no element, which is also 
>> fixed in the PR
>> 
>> All jtreg testsuite tests are run without any regression.
>
> Prasanta Sadhukhan has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Test fix

I do not think this makes a difference. Well, the first insertion does make a 
difference: it changes the text component state so from left-to-right only to 
bidirectional.

The following manipulations just add more Arabic text, so that the entire text 
run is right-to-left.

You want to test the situation where the change from left-to-right to 
bidirectional and component orientation occurs for the first time, and the text 
is inserted in the middle of a text run, in the end of text. You should 
probably, re-create `JTextArea` and `UndoManager` to test such scenarios.

You should also verify the result of undo. If you insert text by one character, 
each undo should remove that character from the text.

If you don't test the result of undo, you don't know whether the text was 
modified as expected or not.

-------------

Changes requested by aivanov (Reviewer).

PR: https://git.openjdk.org/jdk/pull/10446

Reply via email to