Skip to content

database/sql/driver: convertAssignRows not all nilPtr destinations covered in the switch cases #66237

@borissmidt

Description

@borissmidt

Go version

latest

Output of go env in your module/workspace:

latest

What did you do?

I was reading through the source code of database/sql/driver to learn to write better go and out of interest how a language is designed.

What did you see happen?

I suspect no errNilPtr will be returned in
https://github.com/golang/go/blob/master/src/database/sql/convert.go#L272-L278

         case time.Time:
		switch d := dest.(type) {
		case *time.Time:
			*d = s
			return nil
		case *string:
			*d = s.Format(time.RFC3339Nano)
			return nil
		case *[]byte:
			if d == nil {
				return errNilPtr
			}
			*d = []byte(s.Format(time.RFC3339Nano))
			return nil

To confirm this i didn't see any tests for the returning of a nilptr err.

What did you expect to see?

Since dest has to be pointer it would be best to first test if the dest is a nil ptr at the top level of the function.
https://github.com/golang/go/blob/master/src/database/sql/convert.go#L219-L221

func convertAssignRows(dest, src any, rows *Rows) error {
	if d == nil {
		return errNilPtr
	}
	// Common cases, without reflect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions