mjsax commented on code in PR #23156:
URL: https://github.com/apache/kafka/pull/23156#discussion_r3808641078


##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/ProcessorMetadataTest.java:
##########
@@ -161,4 +165,55 @@ public void shouldNotUseCommitFlagForHashcodeAndEquals() {
         assertEquals(metadata1, metadata2);
         assertEquals(metadata1.hashCode(), metadata2.hashCode());
     }
+
+    @Test
+    public void shouldThrowWhenKeySizeExceedsRemainingBytes() {
+        final byte[] keyBytes = "key1".getBytes(StandardCharsets.UTF_8);
+        final int fakeKeySize = keyBytes.length + Long.BYTES + 1; // one past 
what's actually left for this entry
+
+        final ByteBuffer buf = ByteBuffer.allocate(Integer.BYTES + 
Integer.BYTES + keyBytes.length + Long.BYTES);
+        buf.putInt(1);              // entrySize = 1, legitimate
+        buf.putInt(fakeKeySize);    // keySize is too large
+        buf.put(keyBytes);
+        buf.putLong(1L);
+        final byte[] serialized = new byte[buf.position()];
+        buf.position(0);
+        buf.get(serialized);
+
+        assertThrows(BufferUnderflowException.class, () -> 
ProcessorMetadata.deserialize(serialized));
+    }
+
+    @Test
+    public void shouldThrowWhenKeySizeIsNegative() {
+        final ByteBuffer buf = ByteBuffer.allocate(Integer.BYTES + 
Integer.BYTES);
+        buf.putInt(1);    // entrySize = 1, legitimate
+        buf.putInt(-1);   // keySize is negative
+
+        assertThrows(BufferUnderflowException.class, () -> 
ProcessorMetadata.deserialize(buf.array()));

Review Comment:
   This test throws for the wrong reason -- the first condition `entrySize > 
buffer.remaining() / (Integer.BYTES + Long.BYTES)` fires first.



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/ProcessorMetadata.java:
##########
@@ -52,9 +53,15 @@ public static ProcessorMetadata deserialize(final byte[] 
metaDataBytes) {
 
         final ByteBuffer buffer = ByteBuffer.wrap(metaDataBytes);
         final int entrySize = buffer.getInt();
+        if (entrySize < 0 || entrySize > buffer.remaining() / (Integer.BYTES + 
Long.BYTES)) {
+            throw new BufferUnderflowException();
+        }
         final Map<String, Long> metadata = new HashMap<>(entrySize);
         for (int i = 0; i < entrySize; i++) {
             final int keySize = buffer.getInt();
+            if (keySize < 0 || keySize > buffer.remaining() - Long.BYTES) {

Review Comment:
   Seems the `- Long.BYTES` doesn't really make a big difference for the check. 
Should we remove it to keep the code simpler?



##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/ProcessorMetadata.java:
##########
@@ -52,9 +53,15 @@ public static ProcessorMetadata deserialize(final byte[] 
metaDataBytes) {
 
         final ByteBuffer buffer = ByteBuffer.wrap(metaDataBytes);
         final int entrySize = buffer.getInt();
+        if (entrySize < 0 || entrySize > buffer.remaining() / (Integer.BYTES + 
Long.BYTES)) {
+            throw new BufferUnderflowException();

Review Comment:
   We are checking upper and lower bound, so "underflow" seems not to be the 
right exception type? Maybe just using a generic `SerializationException` might 
be better?



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