Move the second part of the nil check earlier in the function. This fixes a panic under certain conditions.

1. arg is a pointer to nil
2. The type of arg implements BinaryEncoder or TextEncoder
3. The implementation of that interface uses a non-pointer receiver
pull/623/head
jaltavilla 2019-10-03 18:31:55 -04:00
parent 0a50ae89be
commit 9e94aa87b1
1 changed files with 10 additions and 6 deletions

View File

@ -67,6 +67,13 @@ func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, o
return nil, nil
}
refVal := reflect.ValueOf(arg)
argIsPtr := refVal.Kind() == reflect.Ptr
if argIsPtr && refVal.IsNil() {
return nil, nil
}
if eqb.paramValueBytes == nil {
eqb.paramValueBytes = make([]byte, 0, 128)
}
@ -100,12 +107,9 @@ func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, o
return []byte(arg), nil
}
refVal := reflect.ValueOf(arg)
if refVal.Kind() == reflect.Ptr {
if refVal.IsNil() {
return nil, nil
}
if argIsPtr {
// We have already checked that arg is not pointing to nil,
// so it is safe to dereference here.
arg = refVal.Elem().Interface()
return eqb.encodeExtendedParamValue(ci, oid, arg)
}