Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close connection on ErrPktSync and ErrPktSyncMul #1473

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Maciej Zimnoch <maciej.zimnoch at codilime.com>
Michael Woolnough <michael.woolnough at gmail.com>
Nathanial Murphy <nathanial.murphy at gmail.com>
Nicola Peduzzi <thenikso at gmail.com>
Oliver Bone <owbone at github.com>
Olivier Mengué <dolmen at cpan.org>
oscarzhao <oscarzhaosl at gmail.com>
Paul Bonser <misterpib at gmail.com>
Expand Down
1 change: 1 addition & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {

// check packet sync [8 bit]
if data[3] != mc.sequence {
mc.Close()
if data[3] > mc.sequence {
return nil, ErrPktSyncMul
}
Expand Down
52 changes: 28 additions & 24 deletions packets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,34 @@ func TestReadPacketSingleByte(t *testing.T) {
}

func TestReadPacketWrongSequenceID(t *testing.T) {
conn := new(mockConn)
mc := &mysqlConn{
buf: newBuffer(conn),
}

// too low sequence id
conn.data = []byte{0x01, 0x00, 0x00, 0x00, 0xff}
conn.maxReads = 1
mc.sequence = 1
_, err := mc.readPacket()
if err != ErrPktSync {
t.Errorf("expected ErrPktSync, got %v", err)
}

// reset
conn.reads = 0
mc.sequence = 0
mc.buf = newBuffer(conn)

// too high sequence id
conn.data = []byte{0x01, 0x00, 0x00, 0x42, 0xff}
_, err = mc.readPacket()
if err != ErrPktSyncMul {
t.Errorf("expected ErrPktSyncMul, got %v", err)
for _, testCase := range []struct {
ClientSequenceID byte
ServerSequenceID byte
ExpectedErr error
}{
{
ClientSequenceID: 1,
ServerSequenceID: 0,
ExpectedErr: ErrPktSync,
},
{
ClientSequenceID: 0,
ServerSequenceID: 0x42,
ExpectedErr: ErrPktSyncMul,
},
} {
conn, mc := newRWMockConn(testCase.ClientSequenceID)

conn.data = []byte{0x01, 0x00, 0x00, testCase.ServerSequenceID, 0xff}
_, err := mc.readPacket()
if err != testCase.ExpectedErr {
t.Errorf("expected %v, got %v", testCase.ExpectedErr, err)
}

// connection should not be returned to the pool in this state
if mc.IsValid() {
t.Errorf("expected IsValid() to be false")
}
}
}

Expand Down