Skip to content

Commit

Permalink
tidb: Fix panic in driverStmt.Query
Browse files Browse the repository at this point in the history
The prepared statement maybe not query, so the resultset maybe nil.
  • Loading branch information
shenli committed Sep 8, 2015
1 parent 1f77496 commit 67d64d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func newDriverConn(sess *session, d *sqlDriver, schema string) (driver.Conn, err

// Prepare returns a prepared statement, bound to this connection.
func (c *driverConn) Prepare(query string) (driver.Stmt, error) {
stmtID, paramCount, _, err := c.s.PrepareStmt(query)
stmtID, paramCount, fields, err := c.s.PrepareStmt(query)
if err != nil {
return nil, err
}
Expand All @@ -196,6 +196,7 @@ func (c *driverConn) Prepare(query string) (driver.Stmt, error) {
query: query,
stmtID: stmtID,
paramCount: paramCount,
isQuery: fields != nil,
}
c.stmts[query] = s
return s, nil
Expand Down Expand Up @@ -460,6 +461,7 @@ type driverStmt struct {
query string
stmtID uint32
paramCount int
isQuery bool
}

// Close closes the statement.
Expand Down Expand Up @@ -505,6 +507,14 @@ func (s *driverStmt) Query(args []driver.Value) (driver.Rows, error) {
if err != nil {
return nil, errors.Trace(err)
}
if rs == nil {
if s.isQuery {
return nil, errors.Trace(errNoResult)
} else {
// The statement is not a query.
return nil, nil
}
}
return newdriverRows(rs), nil
}

Expand Down
1 change: 0 additions & 1 deletion stmt/stmts/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func (s *DeleteStmt) hitWhere(ctx context.Context, t table.Table, data []interfa
for _, col := range t.Cols() {
m[col.Name.L] = data[col.Offset]
}

ok, err := expressions.EvalBoolExpr(ctx, s.Where, m)
if err != nil {
return false, errors.Trace(err)
Expand Down
15 changes: 15 additions & 0 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ func (s *testMainSuite) TestDriverPrepare(c *C) {
mustExec(c, testDB, s.dropDBSQL)
}

// Testcase for delete panic
func (s *testMainSuite) TestDeletePanic(c *C) {
db, err := sql.Open("tidb", "memory://test")
defer db.Close()
_, err = db.Exec("create table t (c int)")
c.Assert(err, IsNil)
_, err = db.Exec("insert into t values (1), (2), (3)")
c.Assert(err, IsNil)
_, err = db.Query("delete from `t` where `c` = ?", 1)
c.Assert(err, IsNil)
_, err = db.Query("delete from `t` where `c` = ?", 2)
c.Assert(err, IsNil)
}

func sessionExec(c *C, se Session, sql string) ([]rset.Recordset, error) {
se.Execute("BEGIN;")
r, err := se.Execute(sql)
Expand Down Expand Up @@ -564,6 +578,7 @@ func (s *testSessionSuite) TestSelectForUpdate(c *C) {
err = se2.Close()
c.Assert(err, IsNil)
}

func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
c.Assert(err, IsNil)
Expand Down

0 comments on commit 67d64d6

Please sign in to comment.