git-ai-coder commented on code in PR #23593:
URL: https://github.com/apache/kafka/pull/23593#discussion_r4347925512
##########
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:
@squah-confluent Thanks for the review and suggestions! I have updated the
PR to match the existing test conventions:
- Added the x10 byte constant to ByteUtilsTest.
- Tightened testInvalidVarint() by testing the exact 0x10 boundary on both
ByteBuffer (via duplicate()) and InputStream (via ByteBufferInputStream), with
single-line assertions.
- Reverted the extra throws IOException and comment changes.
All client unit tests and style checks (checkstyleMain, checkstyleTest) pass
cleanly.
--
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]