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

fix 'invalid compressed sequence' error, issue #871 #872

Merged
merged 1 commit into from
May 8, 2024
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
11 changes: 11 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ func (s *clientTestSuite) TestConn_Ping() {
require.NoError(s.T(), err)
}

func (s *clientTestSuite) TestConn_Compress() {
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
conn, err := Connect(addr, *testUser, *testPassword, "", func(conn *Conn) {
conn.SetCapability(mysql.CLIENT_COMPRESS)
})
require.NoError(s.T(), err)

_, err = conn.Execute("SELECT VERSION()")
require.NoError(s.T(), err)
}

func (s *clientTestSuite) TestConn_SetCapability() {
caps := []uint32{
mysql.CLIENT_LONG_PASSWORD,
Expand Down
12 changes: 11 additions & 1 deletion packet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Conn struct {
compressedHeader [7]byte

compressedReader io.Reader

compressedReaderActive bool
}

func NewConn(conn net.Conn) *Conn {
Expand Down Expand Up @@ -106,12 +108,19 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
}()

if c.Compression != MYSQL_COMPRESS_NONE {
if c.compressedReader == nil {
// it's possible that we're using compression but the server response with a compressed
// packet with uncompressed length of 0. In this case we leave compressedReader nil. The
// compressedReaderActive flag is important to track the state of the reader, allowing
// for the compressedReader to be reset after a packet write. Without this flag, when a
// compressed packet with uncompressed length of 0 is read, the compressedReader would
// be nil, and we'd incorrectly attempt to read the next packet as compressed.
if !c.compressedReaderActive {
var err error
c.compressedReader, err = c.newCompressedPacketReader()
if err != nil {
return nil, err
}
c.compressedReaderActive = true
}
}

Expand Down Expand Up @@ -312,6 +321,7 @@ func (c *Conn) WritePacket(data []byte) error {
return errors.Wrapf(ErrBadConn, "Write failed. only %v bytes written, while %v expected", n, len(data))
}
c.compressedReader = nil
c.compressedReaderActive = false
default:
return errors.Wrapf(ErrBadConn, "Write failed. Unsuppored compression algorithm set")
}
Expand Down