qbs icon indicating copy to clipboard operation
qbs copied to clipboard

Panic in qbs.Count wrt row.Scan on qbs.go:610

Open codemac opened this issue 10 years ago • 0 comments

There is a panic in qbs.Count when the rows returned to count don't exist (because the table doesn't exist yet, the connection hasn't come up yet, etc).

2015/02/10 01:46:58 setting up DB: postgres://postgres:seekret@localhost/candidate?sslmode=disable
qbs:2015/02/10 01:46:58 pq: the database system is starting up
qbs:2015/02/10 01:46:58 pq: the database system is starting up
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x6ba924]

goroutine 26 [running]:
runtime.panic(0x863220, 0xb4ddd3)
        /opt/go/src/pkg/runtime/panic.c:279 +0xf5
database/sql.(*Row).Scan(0x0, 0x7f715abf8d90, 0x1, 0x1, 0x0, 0x0)
        /opt/go/src/pkg/database/sql/sql.go:1636 +0x44
github.com/coocood/qbs.(*Qbs).Count(0xc208060880, 0x891fc0, 0xc208029b20, 0x0)
        /go/src/github.com/coocood/qbs/qbs.go:610 +0x293

If you look at qbs.go:610, you'll see that it calls the following:

if q.criteria.condition != nil {
        conditionSql, args := q.criteria.condition.Merge()
        query += " WHERE " + conditionSql
        row = q.QueryRow(query, args...)
} else {
        row = q.QueryRow(query)
}
var count int64
err := row.Scan(&count)

And QueryRow can return a nil row, thus calling the row.Scan with a nil row value:

func (q *Qbs) QueryRow(query string, args ...interface{}) *sql.Row {
        q.log(query, args...)
        query = q.Dialect.substituteMarkers(query)
        stmt, err := q.prepare(query)
        if err != nil {
                q.updateTxError(err)
                return nil
        }
        return stmt.QueryRow(args...)
}

In this case q.prepare is failing, as postgres is still coming up. Thus return nil is called, and not checked in the callers of QueryRow. I see this as a bug in:

  • ContainsValue on line 578
  • Count on line 610

Whereas doQueryRow returns the errors all the way out. I'm pushing a pull request that fixes this up a bit in a moment.

codemac avatar Feb 10 '15 01:02 codemac