Skip to content

ConvertValue() panics when given a nil pointer to a type that implements driver.Valuer with a value receiver #739

Closed
@yareid

Description

@yareid

Issue description

Given a type that implements driver.Valuer using a value receiver. (e.g. this one). Then passing a nil pointer to that type as an argument to db.Exec will cause mysql.converter.ConvertValue() to panic.

This is a regression. Previously this situation would result in a NULL argument.

Analysis

There is a lengthy discussion of the same issue (resolved some time ago) in database/sql here with their fix here.

I think the problem is the test here added in #710. The interface is not nil and the type assertion succeeds, resulting in a call to Value() with a nil receiver which is not okay because Value() was implemented on the type with a value receiver not a pointer receiver.

Example code

Add the following failing case to driver_test.go TestValuerWithValidation() here.

if _, err := dbt.db.Exec("INSERT INTO testValuer VALUES (?)", (*testValuerWithValidation)(nil)); err != nil {
	dbt.Errorf("Failed to check typed nil")
}

Error log

--- FAIL: TestValuerWithValidation (0.03s)
panic: value method github.com/go-sql-driver/mysql.testValuerWithValidation.Value called using nil *testValuerWithValidation pointer [recovered]
	panic: value method github.com/go-sql-driver/mysql.testValuerWithValidation.Value called using nil *testValuerWithValidation pointer

goroutine 836 [running]:
testing.tRunner.func1(0xc42011c3c0)
	/usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:711 +0x2d2
panic(0x1270900, 0xc4202df190)
	/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491 +0x283
github.com/go-sql-driver/mysql.(*testValuerWithValidation).Value(0x0, 0x126c480, 0x0, 0x240a298, 0x0)
	<autogenerated>:1 +0x7c
github.com/go-sql-driver/mysql.converter.ConvertValue(0x126c480, 0x0, 0x14b2458, 0x0, 0x9, 0x1000)
	/Users/AndrewReid/Code/go/src/github.com/go-sql-driver/mysql/statement.go:142 +0x8d4
github.com/go-sql-driver/mysql.(*mysqlConn).CheckNamedValue(0xc4201f35c0, 0xc4203277d0, 0x1280801, 0x20a0028)
	/Users/AndrewReid/Code/go/src/github.com/go-sql-driver/mysql/connection_go18.go:200 +0x3c
database/sql/driver.(NamedValueChecker).CheckNamedValue-fm(0xc4203277d0, 0x0, 0x0)
	/usr/local/Cellar/go/1.9.2/libexec/src/database/sql/convert.go:180 +0x39

Configuration

Driver version (or git SHA): 2cc627a

Go version: go1.9.2 darwin/amd64

Server version: MySQL 5.7.19

Server OS: OSX 10.11.6 and also Ubuntu Trusty (Travis-CI)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions