Repository navigation
Description
Describe the bug
An unprepared text command accepts a VarChar output parameter with Size unset when an initial value is provided, but the output and reuse behavior are inconsistent:
Output: the first execution returns an empty string instead of the assigned value. The second execution throws an invalid-size exception.InputOutput: the first execution returns the assigned value, but the second execution throws the same exception, even though the parameter still has a nonempty value and its publicSizegetter reports a positive inferred length.
Both behaviors were reproduced on current main and on PR #4906. This is an existing issue, not a regression attributed to that PR.
Correction to the original report: The previously described progressive truncation/reset cycle was an incorrect prediction based on declaration sizing alone. Executing the complete reproduction disproved it: the second execution fails instead of continuing a cycle.
Exception message:
System.InvalidOperationException:
String[1]: the Size property has an invalid size of 0.
Stack trace:
Not captured in the reproduction log.
To reproduce
Create a .NET 10 console application referencing Microsoft.Data.SqlClient built from one of the revisions listed below. Set SQLCLIENT_REPRO_CONNECTION_STRING directly in your environment to a connection string for a database with default collation Latin1_General_100_CI_AS_SC_UTF8.
The reproduction uses managed networking on Windows, an unprepared text command, and synchronous execution. No tables or stored procedures are required.
using System;
using System.Data;
using System.Text;
using Microsoft.Data.SqlClient;
AppContext.SetSwitch(
"Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows", true);
string connectionString =
Environment.GetEnvironmentVariable("SQLCLIENT_REPRO_CONNECTION_STRING")
?? throw new InvalidOperationException(
"Set SQLCLIENT_REPRO_CONNECTION_STRING to a UTF-8 database connection string.");
using var connection = new SqlConnection(connectionString);
connection.Open();
foreach (ParameterDirection direction in new[]
{
ParameterDirection.Output,
ParameterDirection.InputOutput
})
{
using var command = connection.CreateCommand();
command.CommandText = "SET @output = @source;";
var source = command.Parameters.Add("@source", SqlDbType.NVarChar, -1);
source.Value = new string('\u00e9', 4);
var output = command.Parameters.Add("@output", SqlDbType.VarChar);
output.Direction = direction;
output.Value = "abcdefgh";
// Leave Size unset and do not call Prepare().
Console.WriteLine(direction);
for (int execution = 1; execution <= 3; execution++)
{
Console.WriteLine(
$"Execution {execution}: Size before execution = {output.Size}");
try
{
command.ExecuteNonQuery();
string result = (string)output.Value;
Console.WriteLine(
$"Returned characters={result.Length}, " +
$"bytes={Encoding.UTF8.GetByteCount(result)}");
}
catch (InvalidOperationException exception)
{
Console.WriteLine(exception.Message);
break;
}
}
}The command always assigns four é characters, requiring eight UTF-8 bytes. The initial parameter value also occupies eight bytes.
Observed results on both tested revisions:
| Direction | Execution | Public Size before execution | Result |
|---|---|---|---|
| Output | 1 | 8 | Empty string: 0 characters, 0 bytes |
| Output | 2 | 0 | InvalidOperationException: Size has an invalid size of 0 |
| InputOutput | 1 | 8 | Complete value: 4 characters, 8 bytes |
| InputOutput | 2 | 4 | InvalidOperationException: Size has an invalid size of 0 |
The original varying-value sequence was also executed: assign two é characters, then four, then an empty string, then longer values. Both unsized directions fail on the second execution, so the remaining sequence is never reached.
Control: With an explicit output.Size = 8, both directions successfully complete all six executions of that varying-value sequence, returning the requested values, including an empty value followed by a longer value.
Expected behavior
The contract for unsized output parameters should be consistent:
- If an initial value is accepted as sufficient to infer output capacity, the first execution should not silently return an empty string, and subsequent executions should not fail with a zero-size error while a usable inferred capacity remains.
- If an explicit output
Sizeis required, reject the unsupported configuration consistently before executing the first command rather than accepting it and producing empty output or failing only on reuse.
This report does not propose changing whether explicit ANSI Size means bytes or characters.
Further technical details
Microsoft.Data.SqlClient version: Built from source; reproduced independently on:
mainat8dae63509ca17fa5b345596f7f3752bea7a4defd.- PR Fix multibyte ANSI parameter sizing across RPC and encrypted metadata #4906 at
5054b26ae88397917d361903528daae675d6fce3.
.NET target: net10.0; .NET SDK 10.0.403.
SQL Server version: 17.0.1145.4. Reproductions used a temporary database with default collation Latin1_General_100_CI_AS_SC_UTF8.
Operating system: Windows. Managed SNI explicitly enabled; connection pooling disabled in the executed reproduction.
Additional context
The original analysis considered the generated SQL declaration but did not account for the complete RPC output-capacity and command-reuse paths. The actual driver executions above supersede the original predicted cycle.
The PR's added output-direction characterization tests expect the same first-execution results and second-execution exception.
Validation: The same reproduction source was run against separate source builds of both revisions. Both the constant-value and varying-value scenarios were executed, together with explicit-size controls. Temporary databases were removed afterward. No particular released package version or additional platform/SNI combination has been verified.
Workaround verified in this reproduction: specify an explicit Size = 8, sufficient for the eight-byte UTF-8 result. Choose an explicit capacity appropriate for the actual expected data.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
[-]Unsized varchar output parameters change capacity on command reuse, causing history-dependent truncation[/-][+]Unsized varchar Output parameters return empty values and Output/InputOutput reuse fails with invalid Size[/+]