crypto/ssh: fix several logic errors.

These are the obvious mistakes from my read through. I'll save the
more cosmetic changes for a later CL.

R=dave, kardianos
CC=golang-dev
https://golang.org/cl/6049050
This commit is contained in:
Adam Langley
2012-04-19 11:16:57 -04:00
parent e0c50b633f
commit cfd75bc309
+18 -15
View File
@@ -171,7 +171,7 @@ func (c *channel) handleData(data []byte) {
c.myWindow -= uint32(len(data))
for i := 0; i < 2; i++ {
tail := c.head + c.length
if tail > len(c.pendingData) {
if tail >= len(c.pendingData) {
tail -= len(c.pendingData)
}
n := copy(c.pendingData[tail:], data)
@@ -236,16 +236,6 @@ func (c *channel) Read(data []byte) (n int, err error) {
return 0, c.err
}
if c.myWindow <= uint32(len(c.pendingData))/2 {
packet := marshal(msgChannelWindowAdjust, windowAdjustMsg{
PeersId: c.theirId,
AdditionalBytes: uint32(len(c.pendingData)) - c.myWindow,
})
if err := c.serverConn.writePacket(packet); err != nil {
return 0, err
}
}
for {
if c.theySentEOF || c.theyClosed || c.dead {
return 0, io.EOF
@@ -265,16 +255,29 @@ func (c *channel) Read(data []byte) (n int, err error) {
}
if c.length > 0 {
tail := c.head + c.length
if tail > len(c.pendingData) {
tail -= len(c.pendingData)
}
tail := min(c.head + c.length, len(c.pendingData))
n = copy(data, c.pendingData[c.head:tail])
c.head += n
c.length -= n
if c.head == len(c.pendingData) {
c.head = 0
}
windowAdjustment := uint32(len(c.pendingData)-c.length) - c.myWindow
if windowAdjustment >= uint32(len(c.pendingData)/2) {
packet := marshal(msgChannelWindowAdjust, windowAdjustMsg{
PeersId: c.theirId,
AdditionalBytes: windowAdjustment,
})
c.serverConn.lock.Lock()
err = c.serverConn.writePacket(packet)
c.serverConn.lock.Unlock()
if err != nil {
return
}
c.myWindow += windowAdjustment
}
return
}