worryg0d commented on code in PR #1822:
URL: 
https://github.com/apache/cassandra-gocql-driver/pull/1822#discussion_r1960047340


##########
conn.go:
##########
@@ -777,6 +774,172 @@ func (c *Conn) handleTimeout() {
        }
 }
 
+func (c *Conn) recvSegment(ctx context.Context) error {
+       var (
+               frame           []byte
+               isSelfContained bool
+               err             error
+       )
+
+       // Read frame based on compression
+       if c.compressor != nil {
+               frame, isSelfContained, err = readCompressedSegment(c.r, 
c.compressor)
+       } else {
+               frame, isSelfContained, err = readUncompressedSegment(c.r)
+       }
+       if err != nil {
+               return err
+       }
+
+       if isSelfContained {
+               return c.processAllFramesInSegment(ctx, bytes.NewReader(frame))
+       }
+
+       head, err := readHeader(bytes.NewReader(frame), c.headerBuf[:])
+       if err != nil {
+               return err
+       }
+
+       const frameHeaderLength = 9
+       buf := bytes.NewBuffer(make([]byte, 0, head.length+frameHeaderLength))
+       buf.Write(frame)
+
+       // Computing how many bytes of message left to read
+       bytesToRead := head.length - len(frame) + frameHeaderLength
+
+       err = c.recvPartialFrames(buf, bytesToRead)
+       if err != nil {
+               return err
+       }
+
+       return c.processFrame(ctx, buf)
+}
+
+// recvPartialFrames reads proto v5 segments from Conn.r and writes decoded 
partial frames to dst.
+// It reads data until the bytesToRead is reached.
+// If Conn.compressor is not nil, it processes Compressed Format segments.
+func (c *Conn) recvPartialFrames(dst *bytes.Buffer, bytesToRead int) error {
+       var (
+               read            int
+               frame           []byte
+               isSelfContained bool
+               err             error
+       )
+
+       for read != bytesToRead {

Review Comment:
   From my point of view, it is not possible.
   
   `bytesToRead` is the amount of bytes left to read from other segments to 
obtain the completed frame when the segment is not self-contained.  Frame has 
[length]( 
https://github.com/apache/cassandra/blob/75ae84534476b7abf1650066898436625bbdcd3a/doc/native_protocol_v5.spec#L224)
 field which indicates the size of the body.
   
   This part of the code is responsible for reading the whole frame if it is 
segmented. By segmented, I mean it is being sent in more than one segment.
   
   On line 798 it read and deserialized a frame header from the first segment. 
The frame header appears **only** in the first segment. After it computed the 
number of bytes of the frame **body** left to read from other segments. The 
number is computed by this equation:
   ```
   FrameBodyLength - ReadFrameLength + FrameHeaderLength,
   ```
   where: 
   1. FrameBodyLength - length of the frame body
   2. ReadFrameLength - length of the frame that was read from the connection
   3. FrameHeaderLength - length of the frame header
   
   Following this over-reading looks not possible here.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to