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

rec pushed a commit to branch 
bugfix/304-Unable-to-deserialize-CAS-if-last-element-on-the-heap-is-an-empty-array
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit be806580c3b81058a7a2a4460a2c318d4ae7a828
Author: Richard Eckart de Castilho <[email protected]>
AuthorDate: Tue Feb 7 15:52:29 2023 +0100

    #304 - Unable to deserialize CAS if last element on the heap is an empty 
array
    
    - Skip all code related to reading the content of an array if the array is 
empty
    - Formatting
---
 .../org/apache/uima/cas/impl/BinaryCasSerDes.java  | 167 +++++++++++----------
 1 file changed, 91 insertions(+), 76 deletions(-)

diff --git 
a/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes.java 
b/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes.java
index f4ec3336f..a1580c736 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes.java
@@ -676,26 +676,31 @@ public class BinaryCasSerDes {
                               // CAS
       }
     } else {
-      if (heap == null)
+      if (heap == null) {
         heap = new Heap();
-      else
+      } else {
         heap.reset();
-      if (byteHeap == null)
+      }
+      if (byteHeap == null) {
         byteHeap = new ByteHeap();
-      else
+      } else {
         byteHeap.reset();
-      if (shortHeap == null)
+      }
+      if (shortHeap == null) {
         shortHeap = new ShortHeap();
-      else
+      } else {
         shortHeap.reset();
-      if (longHeap == null)
+      }
+      if (longHeap == null) {
         longHeap = new LongHeap();
-      else
+      } else {
         longHeap.reset();
-      if (stringHeap == null)
+      }
+      if (stringHeap == null) {
         stringHeap = new StringHeap();
-      else
+      } else {
         stringHeap.reset();
+      }
       clearDeltaOffsets();
     }
 
@@ -795,23 +800,27 @@ public class BinaryCasSerDes {
       // indexed FSs
       int fsindexsz = r.readInt();
       int[] fsindexes = new int[fsindexsz];
-      if (TRACE_DESER)
+      if (TRACE_DESER) {
         System.out.format("BinDes indexedFSs count: %,d%n", fsindexsz);
+      }
       for (int i = 0; i < fsindexsz; i++) {
         fsindexes[i] = r.readInt();
         if (TRACE_DESER) {
-          if (i % 5 == 0)
+          if (i % 5 == 0) {
             System.out.format("%n i: %5d ", i);
+          }
           System.out.format("%15d ", fsindexes[i]);
         }
       }
-      if (TRACE_DESER)
+      if (TRACE_DESER) {
         System.out.println("");
+      }
 
       // byte heap
       int heapsz = r.readInt();
-      if (TRACE_DESER)
+      if (TRACE_DESER) {
         System.out.format("BinDes ByteHeap size: %,d%n", heapsz);
+      }
 
       if (!delta) {
         byteHeap.heap = new byte[Math.max(16, heapsz)]; // must be > 0
@@ -827,8 +836,9 @@ public class BinaryCasSerDes {
 
       // short heap
       heapsz = r.readInt();
-      if (TRACE_DESER)
+      if (TRACE_DESER) {
         System.out.format("BinDes ShortHeap size: %,d%n", heapsz);
+      }
 
       if (!delta) {
         shortHeap.heap = new short[Math.max(16, heapsz)]; // must be > 0
@@ -850,8 +860,9 @@ public class BinaryCasSerDes {
 
       // long heap
       heapsz = r.readInt();
-      if (TRACE_DESER)
+      if (TRACE_DESER) {
         System.out.format("BinDes LongHeap size: %,d%n", heapsz);
+      }
 
       if (!delta) {
         longHeap.heap = new long[Math.max(16, heapsz)]; // must be > 0
@@ -1680,86 +1691,90 @@ public class BinaryCasSerDes {
       }
       if (type.isArray()) {
         final int len = heap.heap[heapIndex + arrayLengthFeatOffset];
-        final int bhi = heap.heap[heapIndex + arrayContentOffset];
-        final int hhi = heapIndex + arrayContentOffset;
 
         fs = baseCas.createArray(type, len);
         csds.addFS(fs, heapIndex);
-        switch (type.getComponentSlotKind()) {
 
-          case Slot_BooleanRef: {
-            boolean[] ba = ((BooleanArray) fs)._getTheArray();
-            for (int ai = 0; ai < len; ai++) {
-              ba[ai] = byteHeap.heap[bhi + ai] == (byte) 1;
+        if (len > 0) {
+          final int bhi = heap.heap[heapIndex + arrayContentOffset];
+          final int hhi = heapIndex + arrayContentOffset;
+
+          switch (type.getComponentSlotKind()) {
+
+            case Slot_BooleanRef: {
+              boolean[] ba = ((BooleanArray) fs)._getTheArray();
+              for (int ai = 0; ai < len; ai++) {
+                ba[ai] = byteHeap.heap[bhi + ai] == (byte) 1;
+              }
+              break;
             }
-            break;
-          }
 
-          case Slot_ByteRef:
-            System.arraycopy(byteHeap.heap, bhi, ((ByteArray) 
fs)._getTheArray(), 0, len);
-            break;
+            case Slot_ByteRef:
+              System.arraycopy(byteHeap.heap, bhi, ((ByteArray) 
fs)._getTheArray(), 0, len);
+              break;
 
-          case Slot_ShortRef:
-            System.arraycopy(shortHeap.heap, bhi, ((ShortArray) 
fs)._getTheArray(), 0, len);
-            break;
+            case Slot_ShortRef:
+              System.arraycopy(shortHeap.heap, bhi, ((ShortArray) 
fs)._getTheArray(), 0, len);
+              break;
 
-          case Slot_LongRef:
-            System.arraycopy(longHeap.heap, bhi, ((LongArray) 
fs)._getTheArray(), 0, len);
-            break;
+            case Slot_LongRef:
+              System.arraycopy(longHeap.heap, bhi, ((LongArray) 
fs)._getTheArray(), 0, len);
+              break;
 
-          case Slot_DoubleRef: {
-            double[] da = ((DoubleArray) fs)._getTheArray();
-            for (int ai = 0; ai < len; ai++) {
-              da[ai] = CASImpl.long2double(longHeap.heap[bhi + ai]);
+            case Slot_DoubleRef: {
+              double[] da = ((DoubleArray) fs)._getTheArray();
+              for (int ai = 0; ai < len; ai++) {
+                da[ai] = CASImpl.long2double(longHeap.heap[bhi + ai]);
+              }
+              break;
             }
-            break;
-          }
 
-          case Slot_Int:
-            System.arraycopy(heap.heap, hhi, ((IntegerArray) 
fs)._getTheArray(), 0, len);
-            break;
+            case Slot_Int:
+              System.arraycopy(heap.heap, hhi, ((IntegerArray) 
fs)._getTheArray(), 0, len);
+              break;
 
-          case Slot_Float: {
-            float[] fa = ((FloatArray) fs)._getTheArray();
-            for (int ai = 0; ai < len; ai++) {
-              fa[ai] = CASImpl.int2float(heap.heap[hhi + ai]);
+            case Slot_Float: {
+              float[] fa = ((FloatArray) fs)._getTheArray();
+              for (int ai = 0; ai < len; ai++) {
+                fa[ai] = CASImpl.int2float(heap.heap[hhi + ai]);
+              }
+              break;
             }
-            break;
-          }
 
-          case Slot_StrRef: {
-            String[] sa = ((StringArray) fs)._getTheArray();
-            for (int ai = 0; ai < len; ai++) {
-              sa[ai] = stringHeap.getStringForCode(heap.heap[hhi + ai]);
+            case Slot_StrRef: {
+              String[] sa = ((StringArray) fs)._getTheArray();
+              for (int ai = 0; ai < len; ai++) {
+                sa[ai] = stringHeap.getStringForCode(heap.heap[hhi + ai]);
+              }
+              break;
             }
-            break;
-          }
 
-          case Slot_HeapRef: {
-            TOP[] fsa = ((FSArray) fs)._getTheArray();
-            for (int ai = 0; ai < len; ai++) {
-              int a = heap.heap[hhi + ai];
-              if (a == 0) {
-                continue;
-              }
-              TOP item = addr2fs.get(a);
-              if (item != null) {
-                fsa[ai] = item;
-              } else {
-                final int aiSaved = ai;
-                final int addrSaved = a;
-                fixups4forwardFsRefs.add(() -> {
-                  fsa[aiSaved] = addr2fs.get(addrSaved);
-                });
+            case Slot_HeapRef: {
+              TOP[] fsa = ((FSArray) fs)._getTheArray();
+              for (int ai = 0; ai < len; ai++) {
+                int a = heap.heap[hhi + ai];
+                if (a == 0) {
+                  continue;
+                }
+                TOP item = addr2fs.get(a);
+                if (item != null) {
+                  fsa[ai] = item;
+                } else {
+                  final int aiSaved = ai;
+                  final int addrSaved = a;
+                  fixups4forwardFsRefs.add(() -> {
+                    fsa[aiSaved] = addr2fs.get(addrSaved);
+                  });
+                }
               }
+              break;
             }
-            break;
-          }
 
-          default:
-            Misc.internalError();
+            default:
+              Misc.internalError();
 
-        } // end of switch
+          } // end of switch
+        }
       } else { // end of arrays
                // start of normal non-array
         CASImpl view = null;

Reply via email to