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

leerho pushed a commit to branch 9.1.X
in repository https://gitbox.apache.org/repos/asf/datasketches-java.git

commit 3930b5ada9ac1a6222aa037255ec101b7a333f52
Author: Lee Rhodes <[email protected]>
AuthorDate: Thu Oct 29 14:54:42 2026 -0700

    ArrayOfNumbersSerDe: check the item count
    
    Each item takes at least one byte (its type code), so check that the input
    can hold numItems items. PR 783 makes the same check in the UTF-8 and UTF-16
    string SerDes.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../datasketches/common/ArrayOfNumbersSerDe.java   |  1 +
 .../common/ArrayOfNumbersSerDeBoundsTest.java      | 45 ++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git 
a/src/main/java/org/apache/datasketches/common/ArrayOfNumbersSerDe.java 
b/src/main/java/org/apache/datasketches/common/ArrayOfNumbersSerDe.java
index 797d8d3b0..d04fb6167 100644
--- a/src/main/java/org/apache/datasketches/common/ArrayOfNumbersSerDe.java
+++ b/src/main/java/org/apache/datasketches/common/ArrayOfNumbersSerDe.java
@@ -127,6 +127,7 @@ public class ArrayOfNumbersSerDe extends 
ArrayOfItemsSerDe<Number> {
   public Number[] deserializeFromMemorySegment(final MemorySegment seg, final 
long offsetBytes, final int numItems) {
     Objects.requireNonNull(seg, "MemorySegment must not be null");
     if (numItems <= 0) { return new Number[0]; }
+    Util.checkBounds(offsetBytes, numItems, seg.byteSize()); //each item has 
at least a 1-byte type code
     final Number[] array = new Number[numItems];
     long offset = offsetBytes;
     for (int i = 0; i < numItems; i++) {
diff --git 
a/src/test/java/org/apache/datasketches/common/ArrayOfNumbersSerDeBoundsTest.java
 
b/src/test/java/org/apache/datasketches/common/ArrayOfNumbersSerDeBoundsTest.java
new file mode 100644
index 000000000..90ba33686
--- /dev/null
+++ 
b/src/test/java/org/apache/datasketches/common/ArrayOfNumbersSerDeBoundsTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.common;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+
+import java.lang.foreign.MemorySegment;
+
+import org.testng.annotations.Test;
+
+/**
+ * Checks that ArrayOfNumbersSerDe does not allocate from an item count that 
the input cannot hold.
+ */
+public class ArrayOfNumbersSerDeBoundsTest {
+
+  @Test
+  public void checkImpossibleItemCount() {
+    final ArrayOfNumbersSerDe serDe = new ArrayOfNumbersSerDe();
+    final MemorySegment seg = 
MemorySegment.ofArray(serDe.serializeToByteArray(new Number[] {1L, 2.0, 3}));
+    final int numItems = 1 << 28; //each item needs at least one byte
+    final long allocated = AllocationTracker.allocatedBytes(() -> 
serDe.deserializeFromMemorySegment(seg, 0, numItems));
+    assertTrue(allocated < (1 << 20), "allocated " + allocated);
+    expectThrows(SketchesArgumentException.class, () -> 
serDe.deserializeFromMemorySegment(seg, 0, numItems));
+    assertEquals(serDe.deserializeFromMemorySegment(seg, 0, 3).length, 3);
+  }
+}


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

Reply via email to