Fix panic when connection dropped in the middle of the sid

This commit is contained in:
greatroar 2021-03-15 17:53:09 +01:00
parent 6181f5c673
commit 053574d9eb
2 changed files with 22 additions and 1 deletions

View File

@ -239,3 +239,21 @@ func TestClientShortPacket(t *testing.T) {
t.Fatalf("expected error: %v, got: %v", errShortPacket, err)
}
}
// Issue #418: panic in clientConn.recv when the sid is incomplete.
func TestClientNoSid(t *testing.T) {
stream := new(bytes.Buffer)
sendPacket(stream, &sshFxVersionPacket{Version: sftpProtocolVersion})
// Next packet has the sid cut short after two bytes.
stream.Write([]byte{0, 0, 0, 10, 0, 0})
c, err := NewClientPipe(stream, &sink{})
if err != nil {
t.Fatal(err)
}
_, err = c.Stat("anything")
if !errors.Is(err, ErrSSHFxConnectionLost) {
t.Fatal("expected ErrSSHFxConnectionLost, got", err)
}
}

View File

@ -84,7 +84,10 @@ func (c *clientConn) recv() error {
if err != nil {
return err
}
sid, _ := unmarshalUint32(data)
sid, _, err := unmarshalUint32Safe(data)
if err != nil {
return err
}
ch, ok := c.getChannel(sid)
if !ok {