Skip to content

Commit be25d96

Browse files
authored
credentials/alts: Add comments to clarify buffer sizing (grpc#8232)
1 parent db81a2c commit be25d96

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

credentials/alts/internal/conn/record.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ func (p *conn) Read(b []byte) (n int, err error) {
166166
panic(fmt.Sprintf("protected buffer length shorter than expected: %d vs %d", len(p.protected), MsgLenFieldSize))
167167
}
168168
oldProtectedBuf := p.protected
169-
p.protected = make([]byte, int(length)+MsgLenFieldSize)
169+
// The new buffer must be able to hold the message length header
170+
// and the entire message.
171+
requiredCapacity := int(length) + MsgLenFieldSize
172+
p.protected = make([]byte, requiredCapacity)
173+
// Copy the contents of the old buffer and set the length of the
174+
// new buffer to the number of bytes already read.
170175
copy(p.protected, oldProtectedBuf)
171176
p.protected = p.protected[:len(oldProtectedBuf)]
172177
}

0 commit comments

Comments
 (0)