Skip to content

Commit

Permalink
types: fix bug where calling String() on a UDT array would panic
Browse files Browse the repository at this point in the history
It seems like I ran into a proto bug, opened
gogo/protobuf#693.

Release note: None
  • Loading branch information
rohany committed Jun 1, 2020
1 parent 187036c commit ec0d894
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/enums
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,14 @@ _collision 100082 0 100083
__collision 100083 100082 0
collision 100084 0 100085
___collision 100085 100084 0

# Regression for #49756.
query TT
SELECT
column_name, column_type
FROM
crdb_internal.table_columns
WHERE
descriptor_name = 'enum_array' AND column_name = 'x'
----
x family:ArrayFamily width:0 precision:0 locale:"" visible_type:0 oid:100064 array_contents:<InternalType:<family:EnumFamily width:0 precision:0 locale:"" visible_type:0 oid:100063 time_precision_is_set:false udt_metadata:<stable_type_id:63 stable_array_type_id:64 > > TypeMeta:<> > time_precision_is_set:false udt_metadata:<stable_type_id:64 stable_array_type_id:0 >
13 changes: 13 additions & 0 deletions pkg/sql/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,19 @@ func (t *T) String() string {
// DebugString returns a detailed dump of the type protobuf struct, suitable for
// debugging scenarios.
func (t *T) DebugString() string {
if t.Family() == ArrayFamily && t.ArrayContents().UserDefined() {
// In the case that this type is an array that contains a user defined
// type, the introspection that protobuf performs to generate a string
// representation will panic if the UserDefinedTypeMetadata field of the
// type is populated. It seems to not understand that fields in the element
// T could be not generated by proto, and panics trying to operate on the
// TypeMeta field of the T. To get around this, we just deep copy the T and
// zero out the type metadata to placate proto. See the following issue for
// details: https://github.com/gogo/protobuf/issues/693.
internalTypeCopy := protoutil.Clone(&t.InternalType).(*InternalType)
internalTypeCopy.ArrayContents.TypeMeta = UserDefinedTypeMetadata{}
return internalTypeCopy.String()
}
return t.InternalType.String()
}

Expand Down

0 comments on commit ec0d894

Please sign in to comment.