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

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

Open
codemac opened this issue Feb 10, 2015 · 0 comments
Open

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

codemac opened this issue Feb 10, 2015 · 0 comments

Comments

@codemac
Copy link

codemac commented Feb 10, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant