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


##########
clients/src/main/java/org/apache/kafka/common/serialization/ListDeserializer.java:
##########
@@ -196,7 +195,12 @@ public List<Inner> deserialize(String topic, Headers 
headers, byte[] data) {
                     continue;
                 }
                 byte[] payload = new byte[entrySize];
-                if (dis.read(payload) == -1) {
+                try {
+                    // Use readFully (not read) so a stream truncated 
mid-entry is detected: read(byte[]) may
+                    // return a partial count without reaching EOF, which 
would silently leave the tail of the
+                    // payload zero-filled and hand a corrupted entry to the 
inner deserializer.
+                    dis.readFully(payload);
+                } catch (EOFException e) {

Review Comment:
   @mjsax Good call-out I did weigh that. The reason I went with readFully is a 
corner case where read() != entrySize over-rejects: when the list's last 
element serializes to zero bytes (e.g. a trailing empty, non-null String), the 
final entrySize is 0 and the stream is already at EOF. 
ByteArrayInputStream.read(new byte[0]) returns -1 at EOF rather than 0, so 
read(payload) != entrySize becomes -1 != 0 and throws on a valid list. 
readFully(new byte[0]) performs no read and returns normally.
   
   new DataInputStream(new ByteArrayInputStream(new byte[0])).read(new 
byte[0]); // -> -1, not 0
   
   The current read() == -1 has the same issue, so readFully also fixes 
deserializing a list whose last entry is empty. I've added 
shouldDeserializeListEndingInEmptyEntry to cover it. The EOFException is caught 
locally and mapped straight to the existing SerializationException, so it 
doesn't leak beyond this block — but if you'd still prefer to avoid the 
exception, I can special-case entrySize == 0 alongside the length check.



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