This is an automated email from the ASF dual-hosted git repository.

schor pushed a commit to branch 
bug/UIMA-6168-protect-indexes-index-out-of-bounds
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit 1e2a54a7db920869ae813341ef7d7a49d3401dcb
Author: Marshall Schor <m...@schor.com>
AuthorDate: Wed Jan 8 17:45:29 2020 -0500

    [UIMA-6168] fix array index refs, clarify use cases and logic
---
 .../java/org/apache/uima/cas/impl/CASImpl.java     | 45 ++++++++++++++--------
 .../org/apache/uima/cas/impl/FSsTobeAddedback.java |  2 +-
 2 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java 
b/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
index 35f72b6..6a6b1b6 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
@@ -4812,10 +4812,16 @@ public JCasImpl getJCasImpl() {
     svd.fssTobeAddedback.add(r);
     return r;
   }
-  
-  void dropProtectIndexesLevel () {
-    svd.fssTobeAddedback.remove(svd.fssTobeAddedback.size() -1);
-  }
+
+  // never used
+//  void dropProtectIndexesLevel (FSsTobeAddedback fssTobeAddedback) {
+//    int last_index = svd.fssTobeAddedback.size() -1;
+//    if (last_index < 0) return; 
+//    FSsTobeAddedback last_one = svd.fssTobeAddedback.get(last_index);
+//    if (fssTobeAddedback == last_one) {
+//       svd.fssTobeAddedback.remove(last_index);
+//    }
+//  }
   
   /**
    * This design is to support normal operations where the
@@ -4827,7 +4833,7 @@ public JCasImpl getJCasImpl() {
    *    2) the addbacks are (no longer) in the list
    *         - leave stack alone
    *    3) the addbacks are in the list but not at the end
-   *         - remove it and all later ones     
+   *         - remove it and all later ones, calling addback on each     
    *  
    * If the "withProtectedindexes" approach is used, it guarantees proper 
    * nesting, but the Runnable can't throw checked exceptions.
@@ -4840,17 +4846,26 @@ public JCasImpl getJCasImpl() {
    */
   void addbackModifiedFSs (FSsTobeAddedback addbacks) {
     final List<FSsTobeAddedback> listOfAddbackInfos =  svd.fssTobeAddedback;
+    
+    // case 1: the addbacks are the last in the stack:
     if (listOfAddbackInfos.get(listOfAddbackInfos.size() - 1) == addbacks) {
-      listOfAddbackInfos.remove(listOfAddbackInfos.size());
-    } else {
-      int pos = listOfAddbackInfos.indexOf(addbacks);
-      if (pos >= 0) {
-        for (int i = listOfAddbackInfos.size() - 1; i > pos; i--) {
-          FSsTobeAddedback toAddBack = listOfAddbackInfos.remove(i);
-          toAddBack.addback();
-        }
-      }      
-    }
+      listOfAddbackInfos.remove(listOfAddbackInfos.size() - 1);
+      addbacks.addback();
+      return;
+    } 
+     
+    int pos = listOfAddbackInfos.indexOf(addbacks);
+    
+    // case 2: the addbacks are in the stack, but there are others following it
+    if (pos >= 0) {
+      for (int i = listOfAddbackInfos.size() - 1; i >= pos; i--) {
+        FSsTobeAddedback toAddBack = listOfAddbackInfos.remove(i);
+        toAddBack.addback();
+      }
+      return;
+    }      
+    
+    // case 3: the addbacks are not in the list - just remove them, ignore the 
list
     addbacks.addback();
   }
   
diff --git 
a/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSsTobeAddedback.java 
b/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSsTobeAddedback.java
index fb86b53..5ea9d77 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSsTobeAddedback.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSsTobeAddedback.java
@@ -216,7 +216,7 @@ abstract class FSsTobeAddedback implements 
AutoCloseableNoException {
         }
       }
       clear();
-      cas.dropProtectIndexesLevel();
+//      cas.dropProtectIndexesLevel(this); // callers who use the stack do this
     }
     
     @Override

Reply via email to