{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/8415","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/8415/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/8415/comments","events_url":"https://api.github.com/repos/golang/go/issues/8415/events","html_url":"https://github.com/golang/go/issues/8415","id":51288159,"number":8415,"title":"database/sql: panic inserting a nil pointer when underlying type implements sql.Valuer","user":{"login":"a13xb","id":539049,"avatar_url":"https://avatars.githubusercontent.com/u/539049?v=3","gravatar_id":"","url":"https://api.github.com/users/a13xb","html_url":"https://github.com/a13xb","followers_url":"https://api.github.com/users/a13xb/followers","following_url":"https://api.github.com/users/a13xb/following{/other_user}","gists_url":"https://api.github.com/users/a13xb/gists{/gist_id}","starred_url":"https://api.github.com/users/a13xb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/a13xb/subscriptions","organizations_url":"https://api.github.com/users/a13xb/orgs","repos_url":"https://api.github.com/users/a13xb/repos","events_url":"https://api.github.com/users/a13xb/events{/privacy}","received_events_url":"https://api.github.com/users/a13xb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2014-07-23T17:06:50Z","updated_at":"2016-04-03T17:01:16Z","closed_at":null,"body":"<pre>*What does 'go version' print?*\n\ngo version go1.3 darwin/amd64\n\n*What steps reproduce the problem?*\n\nSee attached sample. It needs Postgres to run, but another DB should work just as well,\nthere shouldn't be driver-dependent code involved.\n\n*What happened?*\n\nPanic in case 6 in the sample program.\n\n{{{\npanic: value method main.MyType.Value called using nil *MyType pointer\n\ngoroutine 16 [running]:\nruntime.panic(0x1c1980, 0xc208000960)\n\t/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/panic.c:279 +0xf5\nmain.(*MyType).Value(0x0, 0x0, 0x0, 0x0, 0x0)\n\t&lt;autogenerated&gt;:1 +0x97\ndatabase/sql/driver.defaultConverter.ConvertValue(0x2053a0, 0x0, 0x0, 0x0, 0x0, 0x0)\n\t/usr/local/Cellar/go/1.3/libexec/src/pkg/database/sql/driver/types.go:219 +0x101\ndatabase/sql.driverArgs(0x0, 0xc208095f20, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0)\n\t/usr/local/Cellar/go/1.3/libexec/src/pkg/database/sql/convert.go:35 +0x1e1\ndatabase/sql.(*DB).exec(0xc208054100, 0x28a1f0, 0x1b, 0xc208095f20, 0x1, 0x1, 0x0, 0x0,\n0x0, 0x0)\n\t/usr/local/Cellar/go/1.3/libexec/src/pkg/database/sql/sql.go:884 +0x1ec\ndatabase/sql.(*DB).Exec(0xc208054100, 0x28a1f0, 0x1b, 0xc208095f20, 0x1, 0x1, 0x0, 0x0,\n0x0, 0x0)\n\t/usr/local/Cellar/go/1.3/libexec/src/pkg/database/sql/sql.go:866 +0xd4\nmain.main()\n\t/Users/alex/g/main.go:60 +0x6b5\n}}}\n\n*What should have happened instead?*\n\nBehaviour of Exec() when handling nil pointers of values that implement sql.Valuer\nshould be consistent with primitive values. I can insert a pointer to string, and if\nit's nil, it will translate to SQL NULL just fine.</pre>\n\n\n\n\n\nAttachments:\n\n1. <a href=\"https://storage.googleapis.com/go-attachment/8415/0/main.go\">main.go</a> (996 bytes)"},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/205009608","html_url":"https://github.com/golang/go/issues/8415#issuecomment-205009608","issue_url":"https://api.github.com/repos/golang/go/issues/8415","id":205009608,"user":{"login":"ianlancetaylor","id":3194333,"avatar_url":"https://avatars.githubusercontent.com/u/3194333?v=3","gravatar_id":"","url":"https://api.github.com/users/ianlancetaylor","html_url":"https://github.com/ianlancetaylor","followers_url":"https://api.github.com/users/ianlancetaylor/followers","following_url":"https://api.github.com/users/ianlancetaylor/following{/other_user}","gists_url":"https://api.github.com/users/ianlancetaylor/gists{/gist_id}","starred_url":"https://api.github.com/users/ianlancetaylor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianlancetaylor/subscriptions","organizations_url":"https://api.github.com/users/ianlancetaylor/orgs","repos_url":"https://api.github.com/users/ianlancetaylor/repos","events_url":"https://api.github.com/users/ianlancetaylor/events{/privacy}","received_events_url":"https://api.github.com/users/ianlancetaylor/received_events","type":"User","site_admin":false},"created_at":"2016-04-03T17:01:16Z","updated_at":"2016-04-03T17:01:16Z","body":"Yes, I think @rsc missed something here.  The database/sql/driver package already relies on the reflect pkacage.  If the value implements `Valuer`, we need to check whether it is a pointer, whether it is nil, and whether the `Value` method is implemented on a value rather than on a pointer.  This all seems doable.  Maybe something like\r\n\r\n    if rv.Kind() == reflect.Ptr && rv.IsNil && rv.Type().Elem().Implements(reflect.TypeOf(Valuer)) {\r\n        return nil, nil\r\n    }\r\n\r\nReopening in case somebody wants to implement this, with a test."}}