github-code-scanning[bot] commented on code in PR #447:
URL: https://github.com/apache/datasketches-java/pull/447#discussion_r1239146348


##########
src/main/java/org/apache/datasketches/common/ArrayOfNumbersSerDe.java:
##########
@@ -45,106 +51,218 @@
   private static final byte DOUBLE_INDICATOR  = 4;
   private static final byte FLOAT_INDICATOR   = 6;
 
+  @Override
+  public byte[] serializeToByteArray(final Number item) {
+    final byte[] byteArr;
+    if (item instanceof Long) {

Review Comment:
   ## Chain of 'instanceof' tests
   
   This if block performs a chain of 6 type tests - consider alternatives, e.g. 
polymorphism or the visitor pattern.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/592)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -590,37 +480,36 @@
     offset += len * Integer.BYTES;
 
     //LOAD MIN, MAX Items FOLLOWED BY ITEMS ARRAY
-    switch (sketch.sketchType) {
-      case DOUBLES_SKETCH : {
-        final KllDoublesSketch dblSk = (KllDoublesSketch)sketch;
-        wmem.putDouble(offset,dblSk.getMinDoubleItem());
-        offset += Double.BYTES;
-        wmem.putDouble(offset, dblSk.getMaxDoubleItem());
-        offset += Double.BYTES;
-        wmem.putDoubleArray(offset, dblSk.getDoubleItemsArray(), 
myLevelsArr[0], sketch.getNumRetained());
-        break;
-      }
-      case FLOATS_SKETCH  : {
-        final KllFloatsSketch fltSk = (KllFloatsSketch)sketch;
-        wmem.putFloat(offset, fltSk.getMinFloatItem());
-        offset += Float.BYTES;
-        wmem.putFloat(offset, fltSk.getMaxFloatItem());
-        offset += Float.BYTES;
-        wmem.putFloatArray(offset, fltSk.getFloatItemsArray(), myLevelsArr[0], 
sketch.getNumRetained());
-        break;
-      }
-      //case ITEMS_SKETCH : break; //TODO
-      default: throw new IllegalStateException("Unknown Sketch Type");
+    if (sketch.sketchType == DOUBLES_SKETCH) {
+      final KllDoublesSketch dblSk = (KllDoublesSketch)sketch;
+      wmem.putDouble(offset,dblSk.getMinDoubleItem());
+      offset += Double.BYTES;
+      wmem.putDouble(offset, dblSk.getMaxDoubleItem());
+      offset += Double.BYTES;
+      wmem.putDoubleArray(offset, dblSk.getDoubleItemsArray(), myLevelsArr[0], 
sketch.getNumRetained());
+    } else if (sketch.sketchType == FLOATS_SKETCH) {
+      final KllFloatsSketch fltSk = (KllFloatsSketch)sketch;
+      wmem.putFloat(offset, fltSk.getMinFloatItem());
+      offset += Float.BYTES;
+      wmem.putFloat(offset, fltSk.getMaxFloatItem());
+      offset += Float.BYTES;
+      wmem.putFloatArray(offset, fltSk.getFloatItemsArray(), myLevelsArr[0], 
sketch.getNumRetained());
+    } else if (sketch.sketchType == ITEMS_SKETCH) {
+
+      final KllItemsSketch<T> itmSk = (KllItemsSketch<T>)sketch;

Review Comment:
   ## Unread local variable
   
   Variable 'KllItemsSketch<T> itmSk' is never read.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/593)



##########
src/main/java/org/apache/datasketches/common/ArrayOfDoublesSerDe.java:
##########
@@ -29,28 +31,58 @@
  */
 public class ArrayOfDoublesSerDe extends ArrayOfItemsSerDe<Double> {
 
+  @Override
+  public byte[] serializeToByteArray(final Double item) {
+    final byte[] byteArr = new byte[Double.BYTES];
+    putDoubleLE(byteArr, 0, item.doubleValue());
+    return byteArr;
+  }
+
   @Override
   public byte[] serializeToByteArray(final Double[] items) {
     final byte[] bytes = new byte[Double.BYTES * items.length];
     final WritableMemory mem = WritableMemory.writableWrap(bytes);
-    long offsetBytes = 0;
+    long offset = 0;
     for (int i = 0; i < items.length; i++) {
-      mem.putDouble(offsetBytes, items[i]);
-      offsetBytes += Double.BYTES;
+      mem.putDouble(offset, items[i]);
+      offset += Double.BYTES;
     }
     return bytes;
   }
 
   @Override
-  public Double[] deserializeFromMemory(final Memory mem, final int length) {
-    Util.checkBounds(0, Double.BYTES, mem.getCapacity());
-    final Double[] array = new Double[length];
-    long offsetBytes = 0;
-    for (int i = 0; i < length; i++) {
-      array[i] = mem.getDouble(offsetBytes);
-      offsetBytes += Double.BYTES;
+  public Double deserializeOneFromMemory(final Memory mem, final long offset) {
+    return mem.getDouble(offset);
+  }
+
+  @Override
+  @Deprecated
+  public Double[] deserializeFromMemory(final Memory mem, final int numItems) {
+    return deserializeFromMemory(mem, 0, numItems);
+  }
+
+  @Override
+  public Double[] deserializeFromMemory(final Memory mem, final long 
offsetBytes, final int numItems) {
+    long offset = offsetBytes;
+    Util.checkBounds(offset, Double.BYTES * numItems, mem.getCapacity());

Review Comment:
   ## Result of multiplication cast to wider type
   
   Potential overflow in [int multiplication](1) before it is converted to long 
by use in an invocation context.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/595)



##########
src/main/java/org/apache/datasketches/common/ArrayOfLongsSerDe.java:
##########
@@ -29,28 +31,57 @@
  */
 public class ArrayOfLongsSerDe extends ArrayOfItemsSerDe<Long> {
 
+  @Override
+  public byte[] serializeToByteArray(final Long item) {
+    final byte[] byteArr = new byte[Long.BYTES];
+    putLongLE(byteArr, 0, item.longValue());
+    return byteArr;
+  }
+
   @Override
   public byte[] serializeToByteArray(final Long[] items) {
     final byte[] bytes = new byte[Long.BYTES * items.length];
     final WritableMemory mem = WritableMemory.writableWrap(bytes);
-    long offsetBytes = 0;
+    long offset = 0;
     for (int i = 0; i < items.length; i++) {
-      mem.putLong(offsetBytes, items[i]);
-      offsetBytes += Long.BYTES;
+      mem.putLong(offset, items[i]);
+      offset += Long.BYTES;
     }
     return bytes;
   }
 
   @Override
-  public Long[] deserializeFromMemory(final Memory mem, final int length) {
-    Util.checkBounds(0, (long)length * Long.BYTES, mem.getCapacity());
-    final Long[] array = new Long[length];
-    long offsetBytes = 0;
-    for (int i = 0; i < length; i++) {
-      array[i] = mem.getLong(offsetBytes);
-      offsetBytes += Long.BYTES;
+  public Long deserializeOneFromMemory(final Memory mem, final long offset) {
+    return mem.getLong(offset);
+  }
+
+  @Override
+  @Deprecated
+  public Long[] deserializeFromMemory(final Memory mem, final int numItems) {
+    return deserializeFromMemory(mem, 0, numItems);
+  }
+
+  @Override
+  public Long[] deserializeFromMemory(final Memory mem, final long 
offsetBytes, final int numItems) {
+    long offset = offsetBytes;
+    Util.checkBounds(offset, Long.BYTES * numItems, mem.getCapacity());

Review Comment:
   ## Result of multiplication cast to wider type
   
   Potential overflow in [int multiplication](1) before it is converted to long 
by use in an invocation context.
   
   [Show more 
details](https://github.com/apache/datasketches-java/security/code-scanning/594)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to