SEPURI-SAI-KRISHNA commented on code in PR #22755:
URL: https://github.com/apache/kafka/pull/22755#discussion_r3632231246


##########
clients/src/test/java/org/apache/kafka/common/serialization/ListDeserializerTest.java:
##########
@@ -354,6 +354,42 @@ public void shouldThrowOnTooLargeEntrySize() {
         );
     }
 
+    @Test
+    public void shouldThrowOnEntryTruncatedMidStream() {
+        // entrySize (10) is within data.length (14) so it passes the bounds 
check, but only 5 payload bytes
+        // actually follow. A single read(byte[]) would return a partial count 
(not -1) and silently leave the
+        // entry zero-padded; readFully must instead detect the truncation and 
fail.
+        final byte[] corruptedData = new byte[] {
+            (byte) 
Serdes.ListSerde.SerializationStrategy.VARIABLE_SIZE.ordinal(),
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, // encodes 
length == 1
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0A, // encodes 
entrySize == 10
+            (byte) 'h', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' // only 
5 of the 10 declared bytes
+        };
+        final ListDeserializer<String> testDeserializer = new 
ListDeserializer<>(ArrayList.class, new StringDeserializer());
+        final SerializationException exception = assertThrows(
+            SerializationException.class,
+            () -> testDeserializer.deserialize(null, corruptedData)
+        );
+        assertEquals(
+            "End of the stream was reached prematurely",
+            exception.getMessage()
+        );
+    }
+
+    @Test
+    public void shouldDeserializeListEndingInEmptyEntry() {
+        // A list whose last element serializes to zero bytes leaves the 
stream at EOF when that final
+        // (empty) entry is read. readFully(new byte[0]) is a no-op there, 
whereas read(new byte[0])
+        // returns -1 at EOF and would be misread as premature truncation.

Review Comment:
   Done used your suggested wording here.



##########
clients/src/test/java/org/apache/kafka/common/serialization/ListDeserializerTest.java:
##########
@@ -354,6 +354,42 @@ public void shouldThrowOnTooLargeEntrySize() {
         );
     }
 
+    @Test
+    public void shouldThrowOnEntryTruncatedMidStream() {
+        // entrySize (10) is within data.length (14) so it passes the bounds 
check, but only 5 payload bytes
+        // actually follow. A single read(byte[]) would return a partial count 
(not -1) and silently leave the
+        // entry zero-padded; readFully must instead detect the truncation and 
fail.
+        final byte[] corruptedData = new byte[] {
+            (byte) 
Serdes.ListSerde.SerializationStrategy.VARIABLE_SIZE.ordinal(),
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, // encodes 
length == 1
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0A, // encodes 
entrySize == 10
+            (byte) 'h', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o' // only 
5 of the 10 declared bytes
+        };
+        final ListDeserializer<String> testDeserializer = new 
ListDeserializer<>(ArrayList.class, new StringDeserializer());
+        final SerializationException exception = assertThrows(
+            SerializationException.class,
+            () -> testDeserializer.deserialize(null, corruptedData)
+        );
+        assertEquals(
+            "End of the stream was reached prematurely",
+            exception.getMessage()
+        );
+    }
+
+    @Test
+    public void shouldDeserializeListEndingInEmptyEntry() {
+        // A list whose last element serializes to zero bytes leaves the 
stream at EOF when that final
+        // (empty) entry is read. readFully(new byte[0]) is a no-op there, 
whereas read(new byte[0])

Review Comment:
   Done used your suggested wording here.



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

Reply via email to