JAL-2541 length check in deleteChars to avoid index out of bounds
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 20 Jun 2017 10:08:09 +0000 (11:08 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 20 Jun 2017 10:08:09 +0000 (11:08 +0100)
src/jalview/datamodel/Sequence.java
test/jalview/datamodel/SequenceTest.java

index c8127eb..5915d7e 100755 (executable)
@@ -1158,9 +1158,9 @@ public class Sequence extends ASequence implements SequenceI
     // the very large sequence case
     int eindex = -1, sindex = -1;
     boolean ecalc = false, scalc = false;
-    for (int s = i; s < j; s++)
+    for (int s = i; s < j && s < sequence.length; s++)
     {
-      if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
+      if (!Comparison.isGap(sequence[s]))
       {
         if (createNewDs)
         {
index 5b755c2..71999e8 100644 (file)
@@ -548,6 +548,21 @@ public class SequenceTest
     assertEquals(1, sq.getStart());
     assertEquals(4, sq.getEnd());
     assertNull(PA.getValue(sq, "datasetSequence"));
+
+    /*
+     * delete more positions than there are
+     */
+    sq = new Sequence("test/8-11", "ABCD");
+    sq.deleteChars(0, 99);
+    assertEquals("", sq.getSequenceAsString());
+    assertEquals(12, sq.getStart()); // = findPosition(99) ?!?
+    assertEquals(11, sq.getEnd());
+
+    sq = new Sequence("test/8-11", "----");
+    sq.deleteChars(0, 99); // ArrayIndexOutOfBoundsException <= 2.10.2
+    assertEquals("", sq.getSequenceAsString());
+    assertEquals(8, sq.getStart());
+    assertEquals(11, sq.getEnd());
   }
 
   @Test(groups = { "Functional" })