Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent handling of empty BLOB slices as parameters #2465

Open
mgravell opened this issue Apr 17, 2024 · 1 comment
Open

Inconsistent handling of empty BLOB slices as parameters #2465

mgravell opened this issue Apr 17, 2024 · 1 comment
Labels
Repro Available For issues that are reproducible with repro provided and need investigation

Comments

@mgravell
Copy link
Member

mgravell commented Apr 17, 2024

using the Offset and Size to control slicing of byte[] parameters gives inconsistent results and throws exceptions in some cases:

using Microsoft.Data.SqlClient;
using System;
using System.Data;
var cs = new SqlConnectionStringBuilder
{
    TrustServerCertificate = true,
    IntegratedSecurity = true,
    InitialCatalog = "master",
    DataSource = "."
};
using var conn = new SqlConnection(cs.ConnectionString);
conn.Open();
// entire array
Console.WriteLine(Test(conn, 10, 0, 10)); // 10 - fine

// slice of array
Console.WriteLine(Test(conn, 10, 1, 8)); // 8 - fine

// small slice of array
Console.WriteLine(Test(conn, 10, 1, 1)); // 1 - fine

// empty array
Console.WriteLine(Test(conn, 0, 0, 0)); // 8000 - wat?

// empty slice of non-empty array
Console.WriteLine(Test(conn, 10, 0, 0)); // 10 - um, nope

// empty slice of non-empty array, non-zero offset
Console.WriteLine(Test(conn, 10, 1, 0)); // fault from TdsParserStateObject.WriteByteArray

static int Test(SqlConnection connection, int arraySize, int offset, int count)
{
    var arr = new byte[arraySize];
    using var cmd = connection.CreateCommand();
    cmd.CommandText = "select datalength(@x)";
    var p = new SqlParameter("@x", SqlDbType.Binary, size: count);
    p.Offset = offset;
    p.Value = arr;
    // have tried setting size after value; no change
    // p.Size = count;
    cmd.Parameters.Add(p);
    return (int)cmd.ExecuteScalar();
}

I'm guessing this is "implicit vs explicit zero", with it treating zero as "just send everything"; at a minimum, IMO, this should mean value.Length - Offset (i.e. "the rest of the buffer"), not value.Length. At the moment I'm working around this by detecting zero-length buffers and swapping out for byte[0] so that it can't get confused, but that leaves me a little worried about that 8000

@JRahnama JRahnama added this to Needs triage in SqlClient Triage Board via automation Apr 17, 2024
@JRahnama JRahnama moved this from Needs triage to Needs Investigation in SqlClient Triage Board Apr 17, 2024
@JRahnama JRahnama added the Repro Available For issues that are reproducible with repro provided and need investigation label Apr 17, 2024
@JRahnama
Copy link
Member

@mgravell thanks for bringing this up. We will look into this and will get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Repro Available For issues that are reproducible with repro provided and need investigation
Projects
SqlClient Triage Board
  
Needs Investigation
Development

No branches or pull requests

2 participants