Skip to content

Commit 4d8f8d1

Browse files
[3.1.2] Backport: Fix CommandText length for stored procedures 1484 (#1909)
[3.1.2] Backport: Fix CommandText length for stored procedures(#1909)
1 parent b9671e8 commit 4d8f8d1

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Microsoft.Data.SqlClient
2727
public sealed partial class SqlCommand : DbCommand, ICloneable
2828
{
2929
private static int _objectTypeCount; // EventSource Counter
30+
private const int MaxRPCNameLength = 1046;
3031
internal readonly int ObjectID = Interlocked.Increment(ref _objectTypeCount); private string _commandText;
3132

3233
private static readonly Func<AsyncCallback, object, IAsyncResult> s_beginExecuteReaderAsync = BeginExecuteReaderAsyncCallback;
@@ -5784,7 +5785,20 @@ private void BuildRPC(bool inSchema, SqlParameterCollection parameters, ref _Sql
57845785
GetRPCObject(0, userParameterCount, ref rpc);
57855786

57865787
rpc.ProcID = 0;
5787-
rpc.rpcName = this.CommandText; // just get the raw command text
5788+
5789+
// TDS Protocol allows rpc name with maximum length of 1046 bytes for ProcName
5790+
// 4-part name 1 + 128 + 1 + 1 + 1 + 128 + 1 + 1 + 1 + 128 + 1 + 1 + 1 + 128 + 1 = 523
5791+
// each char takes 2 bytes. 523 * 2 = 1046
5792+
int commandTextLength = ADP.CharSize * CommandText.Length;
5793+
5794+
if (commandTextLength <= MaxRPCNameLength)
5795+
{
5796+
rpc.rpcName = CommandText; // just get the raw command text
5797+
}
5798+
else
5799+
{
5800+
throw ADP.InvalidArgumentLength(nameof(CommandText), MaxRPCNameLength);
5801+
}
57885802

57895803
SetUpRPCParameters(rpc, inSchema, parameters);
57905804
}

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace Microsoft.Data.SqlClient
3838
public sealed class SqlCommand : DbCommand, ICloneable
3939
{
4040
private static int _objectTypeCount; // EventSource Counter
41+
private const int MaxRPCNameLength = 1046;
4142
internal readonly int ObjectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
4243

4344
private string _commandText;
@@ -6682,7 +6683,19 @@ private void BuildRPC(bool inSchema, SqlParameterCollection parameters, ref _Sql
66826683
int count = CountSendableParameters(parameters);
66836684
GetRPCObject(count, ref rpc);
66846685

6685-
rpc.rpcName = this.CommandText; // just get the raw command text
6686+
// TDS Protocol allows rpc name with maximum length of 1046 bytes for ProcName
6687+
// 4-part name 1 + 128 + 1 + 1 + 1 + 128 + 1 + 1 + 1 + 128 + 1 + 1 + 1 + 128 + 1 = 523
6688+
// each char takes 2 bytes. 523 * 2 = 1046
6689+
int commandTextLength = ADP.CharSize * CommandText.Length;
6690+
6691+
if (commandTextLength <= MaxRPCNameLength)
6692+
{
6693+
rpc.rpcName = CommandText; // just get the raw command text
6694+
}
6695+
else
6696+
{
6697+
throw ADP.InvalidArgumentLength(nameof(CommandText), MaxRPCNameLength);
6698+
}
66866699

66876700
SetUpRPCParameters(rpc, 0, inSchema, parameters);
66886701
}

src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTesting.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="SQL\Common\SystemDataInternals\DataReaderHelper.cs" />
100100
<Compile Include="SQL\Common\SystemDataInternals\TdsParserHelper.cs" />
101101
<Compile Include="SQL\Common\SystemDataInternals\TdsParserStateObjectHelper.cs" />
102+
<Compile Include="SQL\SqlCommand\SqlCommandStoredProcTest.cs" />
102103
<Compile Include="DataCommon\AssemblyResourceManager.cs" />
103104
<Compile Include="DataCommon\DataSourceBuilder.cs" />
104105
<Compile Include="DataCommon\DataTestUtility.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Data;
7+
using Xunit;
8+
9+
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
10+
{
11+
public class SqlCommandStoredProcTest
12+
{
13+
private static readonly string s_tcp_connStr = DataTestUtility.TCPConnectionString;
14+
15+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
16+
public static void ShouldFailWithExceededLengthForSP()
17+
{
18+
string baseCommandText = "random text\u0000\u400a\u7300\u7400\u6100\u7400\u6500\u6d00\u6500\u6e00\u7400\u0000\u0006\u01ff\u0900\uf004\u0000\uffdc\u0001";
19+
string exceededLengthText = baseCommandText + new string(' ', 2000);
20+
using SqlConnection conn = new(s_tcp_connStr);
21+
conn.Open();
22+
using SqlCommand command = new()
23+
{
24+
Connection = conn,
25+
CommandType = CommandType.StoredProcedure,
26+
CommandText = exceededLengthText
27+
};
28+
29+
// It should fail on the driver as the length of RPC is over 1046
30+
// 4-part name 1 + 128 + 1 + 1 + 1 + 128 + 1 + 1 + 1 + 128 + 1 + 1 + 1 + 128 + 1 = 523
31+
// each char takes 2 bytes. 523 * 2 = 1046
32+
Assert.Throws<ArgumentException>(() => command.ExecuteScalar());
33+
34+
command.CommandText = baseCommandText;
35+
var ex = Assert.Throws<SqlException>(() => command.ExecuteScalar());
36+
Assert.StartsWith("Could not find stored procedure", ex.Message);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)