Skip to content

database/sql: panic in DB.QueryRow #61646

Closed
@willfaught

Description

@willfaught

What version of Go are you using (go version)?

$ go version
go version go1.20.6 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/Will/Library/Caches/go-build"
GOENV="/Users/Will/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/Will/Library/Application Support/go/pkg/mod"
GONOPROXY="github.com/FrontRowXP,github.com/frontrowxp"
GONOSUMDB="github.com/FrontRowXP,github.com/frontrowxp"
GOOS="darwin"
GOPATH="/Users/Will/Library/Application Support/go"
GOPRIVATE="github.com/FrontRowXP,github.com/frontrowxp"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.20.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.20.6/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.6"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/Users/Will/Developer/work/backend/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bx/qk0phsxd265fqj512dnnpg080000gn/T/go-build4191768799=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

A:

var channelID int = ...
statement := `SELECT column from table WHERE id = $1`
channel_name := ""
err := db.QueryRow(statement, channelID).Scan(&channel_name)

B:

var userID, username string = ...
statement := `UPDATE users SET external_id=$1 WHERE username=$2 RETURNING id;`
lastInsertId := 0
err := db.QueryRow(statement, userID, username).Scan(&lastInsertId)

What did you expect to see?

No panic.

What did you see instead?

--- FAIL: [...] (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x156dd73]

goroutine 6 [running]:
testing.tRunner.func1.2({0x160a5a0, 0x1cf6e10})
	/usr/local/Cellar/go/1.20.6/libexec/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
	/usr/local/Cellar/go/1.20.6/libexec/src/testing/testing.go:1529 +0x39f
panic({0x160a5a0, 0x1cf6e10})
	/usr/local/Cellar/go/1.20.6/libexec/src/runtime/panic.go:884 +0x213
database/sql.(*DB).conn(0x0, {0x194ad90, 0xc000026088}, 0x1)
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1282 +0x53
database/sql.(*DB).query(0xc000367bf0?, {0x194ad90, 0xc000026088}, {0x16b75d5, 0x2b}, {0xc0003cfca8, 0x1, 0x1}, 0x0?)
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1721 +0x5d
database/sql.(*DB).QueryContext.func1(0x0?)
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1704 +0x55
database/sql.(*DB).retry(0x0?, 0xc0003cfb18)
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1538 +0x47
database/sql.(*DB).QueryContext(0x2a595bd8?, {0x194ad90?, 0xc000026088?}, {0x16b75d5?, 0xc0002b3440?}, {0xc0003cfca8?, 0xc0003cfbd8?, 0x100e067?})
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1703 +0xcc
database/sql.(*DB).QueryRowContext(...)
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1804
database/sql.(*DB).QueryRow(0x19467c0?, {0x16b75d5?, 0x0?}, {0xc0003cfca8?, 0xc0003689c0?, 0xc000070c50?})
	/usr/local/Cellar/go/1.20.6/libexec/src/database/sql/sql.go:1818 +0x4a
[...]
FAIL	[...]	0.199s

database/sql/sql.go:1282:

// conn returns a newly-opened or cached *driverConn.     
func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn, error) {     
  db.mu.Lock() // 1282
  if db.closed {     

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions