You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "mjsax (via GitHub)" <gi...@apache.org> on 2026/06/03 15:29:03 UTC
[PR] MINOR: improve ListDeserializer [kafka]
mjsax opened a new pull request, #22463:
URL: https://github.com/apache/kafka/pull/22463
(no comment)
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: improve ListDeserializer [kafka]
Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #22463:
URL: https://github.com/apache/kafka/pull/22463#discussion_r3354879327
##########
clients/src/main/java/org/apache/kafka/common/serialization/ListDeserializer.java:
##########
@@ -173,12 +176,19 @@ public List<Inner> deserialize(String topic, Headers headers, byte[] data) {
List<Integer> nullIndexList = null;
if (serStrategy == SerializationStrategy.CONSTANT_SIZE) {
// In CONSTANT_SIZE strategy, indexes of null entries are decoded from a null index list
- nullIndexList = deserializeNullIndexList(dis);
+ nullIndexList = deserializeNullIndexList(dis, data.length);
}
final int size = dis.readInt();
+ if (size > data.length) {
+ throw new SerializationException("Corrupted byte[]. The number of list entries cannot be larger than overall number of bytes.");
+ }
List<Inner> deserializedList = createListInstance(size);
for (int i = 0; i < size; i++) {
int entrySize = serStrategy == SerializationStrategy.CONSTANT_SIZE ? primitiveSize : dis.readInt();
+ if (entrySize > data.length) {
Review Comment:
These guards catch oversized counts but not negative ones — readInt() can return a negative entrySize (or size). A negative entrySize falls through to `new byte[entrySize]` below and throws NegativeArraySizeException, and a negative size throws from the list constructor; neither gets wrapped as SerializationException. Worth rejecting `< 0` here too so corrupt input fails consistently?
##########
clients/src/main/java/org/apache/kafka/common/serialization/ListDeserializer.java:
##########
@@ -148,8 +148,11 @@ private SerializationStrategy parseSerializationStrategyFlag(final int serializa
return SerializationStrategy.VALUES[serializationStrategyFlag];
}
- private List<Integer> deserializeNullIndexList(final DataInputStream dis) throws IOException {
+ private List<Integer> deserializeNullIndexList(final DataInputStream dis, final int length) throws IOException {
int nullIndexListSize = dis.readInt();
+ if (nullIndexListSize > length) {
Review Comment:
Could we add a couple of tests that feed a corrupted byte[] (oversized null-index size, oversized entry count, oversized entry size) and assert SerializationException? These three new branches are otherwise uncovered.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: improve ListDeserializer [kafka]
Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax merged PR #22463:
URL: https://github.com/apache/kafka/pull/22463
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: improve ListDeserializer [kafka]
Posted by "lucasbru (via GitHub)" <gi...@apache.org>.
lucasbru commented on code in PR #22463:
URL: https://github.com/apache/kafka/pull/22463#discussion_r3361795100
##########
clients/src/test/java/org/apache/kafka/common/serialization/ListDeserializerTest.java:
##########
@@ -277,4 +276,120 @@ public void shouldPassHeadersToUnderlyingDeserializer() {
verify(mockDeserializer, never()).deserialize(anyString(), any(byte[].class));
}
+ @Test
+ public void shouldThrowOnNegativeLength() {
+ final byte[] corruptedData = new byte[] {
+ (byte) Serdes.ListSerde.SerializationStrategy.VARIABLE_SIZE.ordinal(),
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF // encodes length == -1
+ };
+
+ final ListDeserializer<String> testDeserializer = new ListDeserializer<>(ArrayList.class, new StringDeserializer());
+
+ final SerializationException exception = assertThrows(
+ SerializationException.class,
+ () -> testDeserializer.deserialize(null, corruptedData)
+ );
+ assertEquals(
+ "Corrupted byte[]. The number of list entries cannot be negative.",
+ exception.getMessage()
+ );
+ }
+
+ @Test
+ public void shouldThrowOnTooLargeLength() {
+ final byte[] corruptedData = new byte[] {
+ (byte) Serdes.ListSerde.SerializationStrategy.VARIABLE_SIZE.ordinal(),
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xFF // encodes length 255
+ };
+
+ final ListDeserializer<String> testDeserializer = new ListDeserializer<>(ArrayList.class, new StringDeserializer());
+
+ final SerializationException exception = assertThrows(
+ SerializationException.class,
+ () -> testDeserializer.deserialize(null, corruptedData)
+ );
+ assertEquals(
+ "Corrupted byte[]. The number of list entries cannot be larger than overall number of bytes.",
+ exception.getMessage()
+ );
+ }
+
+ @Test
+ public void shouldThrowOnNegativeEntrySize() {
+ final byte[] corruptedData = new byte[] {
+ (byte) Serdes.ListSerde.SerializationStrategy.VARIABLE_SIZE.ordinal(),
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, // encodes length == 0
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE // encodes entrySize == -2 (-1 would be a valid `null` entry)
+ };
+
+ final ListDeserializer<String> testDeserializer = new ListDeserializer<>(ArrayList.class, new StringDeserializer());
+
+ final SerializationException exception = assertThrows(
+ SerializationException.class,
+ () -> testDeserializer.deserialize(null, corruptedData)
+ );
+ assertEquals(
+ "Corrupted byte[]. A list entry cannot have negative size.",
+ exception.getMessage()
+ );
+ }
+
+ @Test
+ public void shouldThrowOnTooLargeEntrySize() {
+ final byte[] corruptedData = new byte[] {
+ (byte) Serdes.ListSerde.SerializationStrategy.VARIABLE_SIZE.ordinal(),
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, // encodes length == 0
Review Comment:
super nit: That seems to encode 1, not 0.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: improve ListDeserializer [kafka]
Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #22463:
URL: https://github.com/apache/kafka/pull/22463#issuecomment-4635348141
Merged to `trunk` and cherry-picked to `4.3`, `4.2,` `4.1`, `4.0`, and `3.9` branches.
--
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: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org