Skip to content

Commit d38d554

Browse files
Wraith2cheenamalhotra
authored andcommitted
Fix | Allow large UDT buffers (#340)
1 parent 6ade367 commit d38d554

File tree

4 files changed

+74
-55
lines changed

4 files changed

+74
-55
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,12 @@ internal static Exception UDTInvalidSqlType(string typeName)
739739
{
740740
return ADP.Argument(System.SRHelper.GetString(SR.SQLUDT_InvalidSqlType, typeName));
741741
}
742+
743+
internal static Exception UDTInvalidSize(int maxSize, int maxSupportedSize)
744+
{
745+
throw ADP.ArgumentOutOfRange(System.SRHelper.GetString(SR.SQLUDT_InvalidSize, maxSize, maxSupportedSize));
746+
}
747+
742748
internal static Exception InvalidSqlDbTypeForConstructor(SqlDbType type)
743749
{
744750
return ADP.Argument(System.SRHelper.GetString(SR.SqlMetaData_InvalidSqlDbTypeForConstructorFormat, type.ToString()));

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8986,15 +8986,21 @@ internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, boo
89868986
}
89878987
else if (mt.SqlDbType == SqlDbType.Udt)
89888988
{
8989+
Debug.Assert(_isYukon, "Invalid DataType UDT for non-Yukon or later server!");
8990+
8991+
int maxSupportedSize = IsKatmaiOrNewer ? int.MaxValue : short.MaxValue;
89898992
byte[] udtVal = null;
89908993
Format format = Format.Native;
89918994

8992-
Debug.Assert(_isYukon, "Invalid DataType UDT for non-Yukon or later server!");
8995+
if (string.IsNullOrEmpty(param.UdtTypeName))
8996+
{
8997+
throw SQL.MustSetUdtTypeNameForUdtParams();
8998+
}
89938999

89949000
if (!isNull)
89959001
{
89969002
// When writing UDT parameter values to the TDS stream, allow sending byte[] or SqlBytes
8997-
// directly to the server and not rejected as invalid. This allows users to handle
9003+
// directly to the server and not reject them as invalid. This allows users to handle
89989004
// serialization and deserialization logic without having to have SqlClient be aware of
89999005
// the types and without using inefficient text representations.
90009006
if (value is byte[] rawBytes)
@@ -9024,20 +9030,15 @@ internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, boo
90249030
Debug.Assert(null != udtVal, "GetBytes returned null instance. Make sure that it always returns non-null value");
90259031
size = udtVal.Length;
90269032

9027-
//it may be legitimate, but we dont support it yet
9028-
if (size < 0 || (size >= ushort.MaxValue && maxsize != -1))
9029-
throw new IndexOutOfRangeException();
9033+
if (size >= maxSupportedSize && maxsize != -1)
9034+
{
9035+
throw SQL.UDTInvalidSize(maxsize, maxSupportedSize);
9036+
}
90309037
}
90319038

9032-
//if this is NULL value, write special null value
9033-
byte[] lenBytes = BitConverter.GetBytes((long)size);
9034-
9035-
if (string.IsNullOrEmpty(param.UdtTypeName))
9036-
throw SQL.MustSetUdtTypeNameForUdtParams();
9037-
90389039
// Split the input name. TypeName is returned as single 3 part name during DeriveParameters.
90399040
// NOTE: ParseUdtTypeName throws if format is incorrect
9040-
string[] names = SqlParameter.ParseTypeName(param.UdtTypeName, true /* is UdtTypeName */);
9041+
string[] names = SqlParameter.ParseTypeName(param.UdtTypeName, isUdtTypeName: true);
90419042
if (!string.IsNullOrEmpty(names[0]) && TdsEnums.MAX_SERVERNAME < names[0].Length)
90429043
{
90439044
throw ADP.ArgumentOutOfRange(nameof(names));

src/Microsoft.Data.SqlClient/netcore/src/Resources/SR.Designer.cs

Lines changed: 50 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.Data.SqlClient/netcore/src/Resources/SR.resx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
</data>
168168
<data name="ADP_InvalidDataDirectory" xml:space="preserve">
169169
<value>The DataDirectory substitute is not a string.</value>
170-
</data>
170+
</data>
171171
<data name="ADP_InvalidEnumerationValue" xml:space="preserve">
172172
<value>The {0} enumeration value, {1}, is invalid.</value>
173173
</data>
@@ -1851,4 +1851,7 @@
18511851
<data name="TCE_AttestationProtocolNotSpecifiedForGeneratingEnclavePackage" xml:space="preserve">
18521852
<value>Error occured when generating enclave package. Attestation Protocol has not been specified in the connection string, but the query requires enclave computations.</value>
18531853
</data>
1854-
</root>
1854+
<data name="SQLUDT_InvalidSize" xml:space="preserve">
1855+
<value>UDT size must be less than {1}, size: {0}</value>
1856+
</data>
1857+
</root>

0 commit comments

Comments
 (0)