@@ -113,21 +113,36 @@ func TypeIDToOID(id descpb.ID) oid.Oid {
113
113
return oid .Oid (id ) + oidext .CockroachPredefinedOIDMax
114
114
}
115
115
116
- // UserDefinedTypeOIDToID converts a user defined type OID into a
117
- // descriptor ID.
118
- func UserDefinedTypeOIDToID (oid oid.Oid ) descpb.ID {
119
- return descpb .ID (oid ) - oidext .CockroachPredefinedOIDMax
116
+ // MaybeUserDefinedTypeOIDToID converts a user defined type OID into a
117
+ // descriptor ID. OID of a user-defined type must be greater than
118
+ // CockraochPredefinedOIDMax.
119
+ func MaybeUserDefinedTypeOIDToID (oid oid.Oid ) (descpb.ID , bool ) {
120
+ if descpb .ID (oid ) <= oidext .CockroachPredefinedOIDMax {
121
+ return 0 , false
122
+ }
123
+ return descpb .ID (oid ) - oidext .CockroachPredefinedOIDMax , true
124
+ }
125
+
126
+ // MakeOIDRangeError is a helper to create an error message for invalid user-defined OID conversion.
127
+ func MakeOIDRangeError (oid oid.Oid ) error {
128
+ return errors .Newf ("user-defined OID %d is out of range. It must be greater than Max OID: %d." ,
129
+ oid , oidext .CockroachPredefinedOIDMax )
120
130
}
121
131
122
132
// GetTypeDescID gets the type descriptor ID from a user defined type.
123
- func GetTypeDescID (t * types.T ) descpb.ID {
124
- return UserDefinedTypeOIDToID (t .Oid ())
133
+ func GetTypeDescID (t * types.T ) ( descpb.ID , bool ) {
134
+ return MaybeUserDefinedTypeOIDToID (t .Oid ())
125
135
}
126
136
127
137
// GetArrayTypeDescID gets the ID of the array type descriptor from a user
128
138
// defined type.
129
- func GetArrayTypeDescID (t * types.T ) descpb.ID {
130
- return UserDefinedTypeOIDToID (t .UserDefinedArrayOID ())
139
+ func GetArrayTypeDescID (t * types.T ) (descpb.ID , bool ) {
140
+ return MaybeUserDefinedTypeOIDToID (t .UserDefinedArrayOID ())
141
+ }
142
+
143
+ // MakeTypeIDRangeError is a helper to create an error message for invalid type ID conversion.
144
+ func MakeTypeIDRangeError (t * types.T ) error {
145
+ return MakeOIDRangeError (t .Oid ())
131
146
}
132
147
133
148
// TypeDesc implements the Descriptor interface.
@@ -599,10 +614,13 @@ func (desc *immutable) ValidateCrossReferences(
599
614
}
600
615
case descpb .TypeDescriptor_ALIAS :
601
616
if desc .GetAlias ().UserDefined () {
602
- aliasedID := UserDefinedTypeOIDToID (desc .GetAlias ().Oid ())
617
+ aliasedID , isValid := MaybeUserDefinedTypeOIDToID (desc .GetAlias ().Oid ())
603
618
if _ , err := vdg .GetTypeDescriptor (aliasedID ); err != nil {
604
619
vea .Report (errors .Wrapf (err , "aliased type %d does not exist" , aliasedID ))
605
620
}
621
+ if ! isValid {
622
+ vea .Report (errors .Wrapf (err , "aliased type %d is out of range" , aliasedID ))
623
+ }
606
624
}
607
625
}
608
626
@@ -724,7 +742,11 @@ func HydrateTypesInTableDescriptor(
724
742
hydrateCol := func (col * descpb.ColumnDescriptor ) error {
725
743
if col .Type .UserDefined () {
726
744
// Look up its type descriptor.
727
- name , typDesc , err := res .GetTypeDescriptor (ctx , GetTypeDescID (col .Type ))
745
+ td , ok := GetTypeDescID (col .Type )
746
+ if ! ok {
747
+ return MakeTypeIDRangeError (col .Type )
748
+ }
749
+ name , typDesc , err := res .GetTypeDescriptor (ctx , td )
728
750
if err != nil {
729
751
return err
730
752
}
@@ -787,7 +809,11 @@ func (desc *immutable) HydrateTypeInfoWithName(
787
809
case types .ArrayFamily :
788
810
// Hydrate the element type.
789
811
elemType := typ .ArrayContents ()
790
- elemTypName , elemTypDesc , err := res .GetTypeDescriptor (ctx , GetTypeDescID (elemType ))
812
+ id , ok := GetTypeDescID (elemType )
813
+ if ! ok {
814
+ return MakeTypeIDRangeError (elemType )
815
+ }
816
+ elemTypName , elemTypDesc , err := res .GetTypeDescriptor (ctx , id )
791
817
if err != nil {
792
818
return err
793
819
}
@@ -925,9 +951,10 @@ func GetTypeDescriptorClosure(typ *types.T) map[descpb.ID]struct{} {
925
951
if ! typ .UserDefined () {
926
952
return map [descpb.ID ]struct {}{}
927
953
}
954
+ id , _ := GetTypeDescID (typ )
928
955
// Collect the type's descriptor ID.
929
956
ret := map [descpb.ID ]struct {}{
930
- GetTypeDescID ( typ ) : {},
957
+ id : {},
931
958
}
932
959
if typ .Family () == types .ArrayFamily {
933
960
// If we have an array type, then collect all types in the contents.
@@ -937,7 +964,8 @@ func GetTypeDescriptorClosure(typ *types.T) map[descpb.ID]struct{} {
937
964
}
938
965
} else {
939
966
// Otherwise, take the array type ID.
940
- ret [GetArrayTypeDescID (typ )] = struct {}{}
967
+ id , _ := GetArrayTypeDescID (typ )
968
+ ret [id ] = struct {}{}
941
969
}
942
970
return ret
943
971
}
0 commit comments