viirya commented on code in PR #55620:
URL: https://github.com/apache/spark/pull/55620#discussion_r3246767222


##########
common/network-common/src/main/java/org/apache/spark/network/shuffle/streaming/ShuffleChecksum.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.spark.network.shuffle.streaming;
+
+import io.netty.buffer.ByteBuf;
+import java.util.zip.CRC32C;
+import javax.annotation.concurrent.NotThreadSafe;
+
+/**
+ * Helper class for streaming shuffle checksum calculations.
+ */
+@NotThreadSafe
+public final class ShuffleChecksum {
+    private final CRC32C crc = new CRC32C();
+
+    /**
+     * Updates checksum for a specified portion of a ByteBuf message.
+     *
+     * @param message The ByteBuf to calculate checksum for
+     * @param startIndex The index of the first byte to calculate checksum for
+     * @param dataLength The length of the data to calculate checksum for
+     */
+    public void updateChecksum(ByteBuf message, int startIndex, int 
dataLength) {
+        if (startIndex < 0) {
+            throw new IllegalArgumentException(
+                "startIndex must be non-negative: " + startIndex);
+        }
+        if (dataLength < 0) {
+            throw new IllegalArgumentException(
+                "dataLength must be non-negative: " + dataLength);
+        }
+        if (startIndex + dataLength > message.capacity()) {

Review Comment:
   ShuffleChecksum still validates against capacity(), which can include 
unwritten bytes:
   
   ShuffleChecksum.java:47 checks startIndex + dataLength > message.capacity(). 
This is better than relying on Java assertions, but capacity() is not the 
boundary of valid data; it may include unwritten space beyond writerIndex(). 
Since the checksum should cover actual message bytes, this can allow callers to 
accidentally compute CRC over bytes that were never written.
   
   It would be safer to validate against writerIndex(), for example by 
requiring startIndex >= 0, dataLength >= 0, and dataLength <= 
message.writerIndex() - startIndex. It would also be worth adding tests for 
non-zero startIndex, out-of-bounds ranges, and both heap/direct buffers.
   
   
   
   
   
   



-- 
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