2
0

Fix simple protocol empty array and original recursive empty array issue

Original issue https://github.com/jackc/pgtype/issues/68

This crash occurred in the recursive assignment system used to support
multidimensional arrays.

This was fixed in 9639a69d45. However,
that fix incorrectly used nil instead of an empty slice.

In hindsight, it appears the fundamental error is that an assignment to
a slice of a type that is not specified is handled with the recursive /
reflection path. Or another way of looking at it is as an unexpected
feature where []T can now be scanned if individual elements are
assignable to T even if []T is not specifically handled.

But this new reflection / recursive path did not handle empty arrays.

This fix handles the reflection path for an empty slice by allocating an
empty slice.
This commit is contained in:
Jack Christensen
2020-10-31 17:12:16 -05:00
parent 9d7fc8e63a
commit af0ca3a39b
44 changed files with 191 additions and 119 deletions
+7 -4
View File
@@ -190,10 +190,6 @@ func (dst EnumArray) Get() interface{} {
func (src *EnumArray) AssignTo(dst interface{}) error {
switch src.Status {
case Present:
if len(src.Elements) == 0 || len(src.Dimensions) == 0 {
// No values to assign
return nil
}
if len(src.Dimensions) <= 1 {
// Attempt to match to select common types:
switch v := dst.(type) {
@@ -232,6 +228,13 @@ func (src *EnumArray) AssignTo(dst interface{}) error {
value = value.Elem()
}
if len(src.Elements) == 0 {
if value.Kind() == reflect.Slice {
value.Set(reflect.MakeSlice(value.Type(), 0, 0))
return nil
}
}
elementCount, err := src.assignToRecursive(value, 0, 0)
if err != nil {
return err