efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Strings which contain NUL '\0' are truncated on read

Open denisz1 opened this issue 9 years ago • 6 comments

I'm using EF Core with both SQL Server and SQLite.

I can insert a string which contains a NUL character "foo\0bar". It seems to be written correctly.

I can read it back using a regular sql connection. But if I use EF to read it back, I get only "foo".

denisz1 avatar Feb 10 '17 04:02 denisz1

This is by design for Microsoft.Data.Sqlite. We use NUL to determine the end of the string during read.

The SQLite documentation also states:

The result of expressions involving strings with embedded NULs is undefined.

bricelam avatar Feb 10 '17 16:02 bricelam

Note, in 2.0 Microsoft.Data.Sqlite is building on top of SQLitePCL.raw which does the same thing.

bricelam avatar Feb 10 '17 16:02 bricelam

OK by design then. Thanks for clarification.

denisz1 avatar Feb 10 '17 18:02 denisz1

@denisz1 @T18970237136 It looks like this is "fixed" in version 3.0.0

bricelam avatar Sep 18 '19 21:09 bricelam

Hi @bricelam,

in which area was this fixed? When I use Microsoft.Data.Sqlite 3.1.4 and test with a raw INSERT and SELECT using an SqliteCommand, strings still seem to be cut off after a NUL (\0):

    using var memoryDb = new SqliteConnection(new SqliteConnectionStringBuilder {
        DataSource = ":memory:"
    }.ToString());
    memoryDb.Open();

    const string checkValue = "a\0b\0c";

    using var cmd = memoryDb.CreateCommand();
    cmd.CommandText = "CREATE TABLE T1(a TEXT); INSERT INTO T1 (a) VALUES (@P1);";
    cmd.Parameters.AddWithValue("@P1", checkValue);
    cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();

    cmd.CommandText = "SELECT a FROM T1;";
    string strValue = (string)cmd.ExecuteScalar();

    Console.WriteLine("String is equal: " + (strValue == checkValue)); // prints "False"

T18970237136 avatar Jun 08 '20 08:06 T18970237136

@T18970237136 Can you submit a new issue? Comments on closed issues tend to get lost or forgotten.

bricelam avatar Jun 11 '20 23:06 bricelam