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 malformed packet and a panic #655

Merged
merged 1 commit into from
Dec 16, 2021
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
4 changes: 3 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func (s *clientTestSuite) SetUpSuite(c *C) {
c.Fatal(err)
}

_, err = s.c.Execute("CREATE DATABASE IF NOT EXISTS " + *testDB)
var result *mysql.Result
result, err = s.c.Execute("CREATE DATABASE IF NOT EXISTS " + *testDB)
c.Assert(err, IsNil)
c.Assert(result.RowNumber() >= 0, IsTrue)

_, err = s.c.Execute("USE " + *testDB)
c.Assert(err, IsNil)
Expand Down
14 changes: 5 additions & 9 deletions client/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *Conn) handleOKPacket(data []byte) (*Result, error) {
var n int
var pos = 1

r := &Result{Resultset: &Resultset{}}
r := new(Result)

r.AffectedRows, _, n = LengthEncodedInt(data[pos:])
pos += n
Expand Down Expand Up @@ -128,11 +128,8 @@ func (c *Conn) handleAuthResult() error {
return nil // auth already succeeded
}
if data[0] == CACHE_SHA2_FAST_AUTH {
if _, err = c.readOK(); err == nil {
return nil // auth successful
} else {
return err
}
_, err = c.readOK()
return err
} else if data[0] == CACHE_SHA2_FULL_AUTH {
// need full authentication
if c.tlsConfig != nil || c.proto == "unix" {
Expand All @@ -144,9 +141,8 @@ func (c *Conn) handleAuthResult() error {
return err
}
}
if _, err = c.readOK(); err != nil {
return err
}
_, err = c.readOK()
return err
} else {
return errors.Errorf("invalid packet %x", data[0])
}
Expand Down
3 changes: 3 additions & 0 deletions mysql/resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (r *Resultset) Reset(fieldsCount int) {
}

func (r *Resultset) RowNumber() int {
if r == nil {
return 0
}
return len(r.Values)
}

Expand Down