squah-confluent commented on code in PR #23593:
URL: https://github.com/apache/kafka/pull/23593#discussion_r4303179199


##########
clients/src/test/java/org/apache/kafka/common/utils/internals/ByteUtilsTest.java:
##########
@@ -312,10 +312,26 @@ public void testVarlongSerde() throws Exception {
     }
 
     @Test
-    public void testInvalidVarint() {
-        // varint encoding has one overflow byte
+    public void testInvalidVarint() throws IOException {
+        // varint encoding has one overflow byte (6-byte sequence)
         ByteBuffer buf = ByteBuffer.wrap(new byte[] {xFF, xFF, xFF, xFF, xFF, 
x01});
         assertThrows(IllegalArgumentException.class, () -> 
ByteUtils.readVarint(buf));
+
+        // unused bits set in the 5th byte (bits 5-7 must be zero for a valid 
32-bit varint)
+        byte[] unusedBitsEncoding = new byte[] {xFF, xFF, xFF, xFF, 0x1F};
+        assertThrows(IllegalArgumentException.class,
+                () -> 
ByteUtils.readUnsignedVarint(ByteBuffer.wrap(unusedBitsEncoding)));
+        assertThrows(IllegalArgumentException.class,
+                () -> ByteUtils.readUnsignedVarint(new 
ByteArrayInputStream(unusedBitsEncoding)));
+
+        // boundary: 0x10 in the 5th byte has bit 4 set and is also invalid
+        byte[] bit4Encoding = new byte[] {xFF, xFF, xFF, xFF, 0x10};
+        assertThrows(IllegalArgumentException.class,
+                () -> 
ByteUtils.readUnsignedVarint(ByteBuffer.wrap(bit4Encoding)));
+        assertThrows(IllegalArgumentException.class,
+                () -> ByteUtils.readUnsignedVarint(new 
ByteArrayInputStream(bit4Encoding)));
+
+        assertEquals(-1, ByteUtils.readUnsignedVarint(ByteBuffer.wrap(new 
byte[] {xFF, xFF, xFF, xFF, 0x0F})));

Review Comment:
   Suggest tightening this up to match the existing tests:
   
   - revert the "(6-byte sequence)" addition and the unneeded `throws 
IOException`
   - drop the `0x1F` case, which is rejected for the same bit as `0x10`
   - drop the `0x0F` assertion, which duplicates `assertUnsignedVarintSerde(-1, 
...)` in `testUnsignedVarintSerde`
   - use local variables and single-line `assertThrows`, as elsewhere in this 
file
   
   This needs an `x10` constant alongside the others (`private final byte x10 = 
0x10;`). That's outside the diff, so it can't go in the suggestion.
   
   ```suggestion
       public void testInvalidVarint() {
           // varint encoding has one overflow byte
           ByteBuffer buf = ByteBuffer.wrap(new byte[] {xFF, xFF, xFF, xFF, 
xFF, x01});
           assertThrows(IllegalArgumentException.class, () -> 
ByteUtils.readVarint(buf));
   
           // varint encoding has unused bits set in the last byte
           ByteBuffer unusedBitsBuf = ByteBuffer.wrap(new byte[] {xFF, xFF, 
xFF, xFF, x10});
           assertThrows(IllegalArgumentException.class, () -> 
ByteUtils.readUnsignedVarint(unusedBitsBuf.duplicate()));
           InputStream unusedBitsIn = new ByteBufferInputStream(unusedBitsBuf);
           assertThrows(IllegalArgumentException.class, () -> 
ByteUtils.readUnsignedVarint(unusedBitsIn));
   ```
   



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