Skip to content

Commit

Permalink
socket/client.go: Handle panics from pk.Unmarshal()
Browse files Browse the repository at this point in the history
  • Loading branch information
TwistedAsylumMC committed Jul 2, 2022
1 parent 137f7d4 commit e421fe7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion socket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *Client) Authenticated() bool {

// ReadPacket reads a packet from the connection and returns it. The client is expected to prefix the packet
// payload with 4 bytes for the length of the payload.
func (c *Client) ReadPacket() (packet.Packet, error) {
func (c *Client) ReadPacket() (pk packet.Packet, err error) {
var l uint32
if err := binary.Read(c.conn, binary.LittleEndian, &l); err != nil {
return nil, err
Expand All @@ -90,6 +90,11 @@ func (c *Client) ReadPacket() (packet.Packet, error) {
return nil, fmt.Errorf("unknown packet %v", header.PacketID)
}

defer func() {
if recoveredErr := recover(); recoveredErr != nil {
err = fmt.Errorf("%T: %w", pk, recoveredErr.(error))
}
}()
pk.Unmarshal(protocol.NewReader(buf, 0))
if buf.Len() > 0 {
return nil, fmt.Errorf("still have %v bytes unread", buf.Len())
Expand Down

0 comments on commit e421fe7

Please sign in to comment.