Skip to content

Microsoft.Data.Sqlite: SqliteParameter.Size = 0 sends an empty string or blob #39376

Closed

Description

@alaricd

Setting SqliteParameter.Size to 0 truncates the bound value to nothing: a string is sent as "" and a byte[] as an empty blob. 0 is the parameter's default, so code that resets a reused parameter (parameter.Size = 0) silently stores empty values.

command.CommandText = "SELECT $value";
command.Parameters.AddWithValue("$value", "TEST").Size = 0;
command.ExecuteScalar();   // "" - expected "TEST"

What the documentation says

DbParameter.Size: "The default value is inferred from the parameter value." A new SqliteParameter reports Size 0, and when it is left unset the whole value is sent; assigning that same default explicitly should not change what is sent.

Why this is a bug

SqliteParameterBinder.ShouldTruncate treats any value other than -1 as a limit, so 0 truncates to zero characters or bytes. The truncation is silent: no exception, just an empty value stored. Positive sizes (Bind_with_restricted_size_works_on_*) and -1 (Bind_with_sentinel_size_works_on_*) are already covered and are unchanged by treating 0 as "no limit" too.

Reproduced on Microsoft.Data.Sqlite 6.0.36 through 10.0.13. I have a small change with tests.

Found with https://github.com/pengdows/pengdows.AdoNetApiTest.conformance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions