Skip to content

Commit

Permalink
Fix | Allow large UDT buffers (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored and cheenamalhotra committed Dec 5, 2019
1 parent 6ade367 commit d38d554
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,12 @@ internal static Exception UDTInvalidSqlType(string typeName)
{
return ADP.Argument(System.SRHelper.GetString(SR.SQLUDT_InvalidSqlType, typeName));
}

internal static Exception UDTInvalidSize(int maxSize, int maxSupportedSize)
{
throw ADP.ArgumentOutOfRange(System.SRHelper.GetString(SR.SQLUDT_InvalidSize, maxSize, maxSupportedSize));
}

internal static Exception InvalidSqlDbTypeForConstructor(SqlDbType type)
{
return ADP.Argument(System.SRHelper.GetString(SR.SqlMetaData_InvalidSqlDbTypeForConstructorFormat, type.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8986,15 +8986,21 @@ internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, boo
}
else if (mt.SqlDbType == SqlDbType.Udt)
{
Debug.Assert(_isYukon, "Invalid DataType UDT for non-Yukon or later server!");

int maxSupportedSize = IsKatmaiOrNewer ? int.MaxValue : short.MaxValue;
byte[] udtVal = null;
Format format = Format.Native;

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

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

//it may be legitimate, but we dont support it yet
if (size < 0 || (size >= ushort.MaxValue && maxsize != -1))
throw new IndexOutOfRangeException();
if (size >= maxSupportedSize && maxsize != -1)
{
throw SQL.UDTInvalidSize(maxsize, maxSupportedSize);
}
}

//if this is NULL value, write special null value
byte[] lenBytes = BitConverter.GetBytes((long)size);

if (string.IsNullOrEmpty(param.UdtTypeName))
throw SQL.MustSetUdtTypeNameForUdtParams();

// Split the input name. TypeName is returned as single 3 part name during DeriveParameters.
// NOTE: ParseUdtTypeName throws if format is incorrect
string[] names = SqlParameter.ParseTypeName(param.UdtTypeName, true /* is UdtTypeName */);
string[] names = SqlParameter.ParseTypeName(param.UdtTypeName, isUdtTypeName: true);
if (!string.IsNullOrEmpty(names[0]) && TdsEnums.MAX_SERVERNAME < names[0].Length)
{
throw ADP.ArgumentOutOfRange(nameof(names));
Expand Down
91 changes: 50 additions & 41 deletions src/Microsoft.Data.SqlClient/netcore/src/Resources/SR.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/Microsoft.Data.SqlClient/netcore/src/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
</data>
<data name="ADP_InvalidDataDirectory" xml:space="preserve">
<value>The DataDirectory substitute is not a string.</value>
</data>
</data>
<data name="ADP_InvalidEnumerationValue" xml:space="preserve">
<value>The {0} enumeration value, {1}, is invalid.</value>
</data>
Expand Down Expand Up @@ -1851,4 +1851,7 @@
<data name="TCE_AttestationProtocolNotSpecifiedForGeneratingEnclavePackage" xml:space="preserve">
<value>Error occured when generating enclave package. Attestation Protocol has not been specified in the connection string, but the query requires enclave computations.</value>
</data>
</root>
<data name="SQLUDT_InvalidSize" xml:space="preserve">
<value>UDT size must be less than {1}, size: {0}</value>
</data>
</root>

0 comments on commit d38d554

Please sign in to comment.