Skip to content

Commit

Permalink
Merge pull request #655 from lance6716/fix
Browse files Browse the repository at this point in the history
Fix malformed packet and a panic
  • Loading branch information
lance6716 authored Dec 16, 2021
2 parents 400546f + 4ad9b0c commit 0c0b817
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
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

0 comments on commit 0c0b817

Please sign in to comment.