Skip to content

Commit

Permalink
in the provider api, rm the utf8z overloads for sqlite3_prepare_v2/v3…
Browse files Browse the repository at this point in the history
…. these overloads were not memory safe, as they returned a span made from a pointer obtained from a fixed block. technically, this is a breaking change, but it is likely that nothing was using those overloads. my own test suite was not, and Microsoft.Data.Sqlite was not. there was discussion of the problem with these overloads in #321.  additional detective work on this happened while trying to figure out #430.
  • Loading branch information
ericsink committed Aug 16, 2021
1 parent 90872de commit 264be3a
Show file tree
Hide file tree
Showing 24 changed files with 31 additions and 409 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Copyright>Copyright 2014-2021 SourceGear, LLC</Copyright>
<Company>SourceGear</Company>
<Authors>Eric Sink</Authors>
<Version>2.0.5-pre20210816092746</Version>
<Version>2.0.5-pre20210816151822</Version>
<AssemblyVersion>2.0.5.1323</AssemblyVersion>
<FileVersion>2.0.5.1323</FileVersion>
<Description>SQLitePCLRaw is a Portable Class Library (PCL) for low-level (raw) access to SQLite</Description>
Expand Down
2 changes: 0 additions & 2 deletions src/SQLitePCLRaw.core/isqlite3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ public interface ISQLite3Provider

int sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sql, out IntPtr stmt, out ReadOnlySpan<byte> remain);
int sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stmt, out ReadOnlySpan<byte> remain);
int sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stmt, out utf8z remain);
int sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stmt, out utf8z remain);
int sqlite3_step(sqlite3_stmt stmt);
int sqlite3_finalize(IntPtr stmt);
int sqlite3_reset(sqlite3_stmt stmt);
Expand Down
44 changes: 12 additions & 32 deletions src/SQLitePCLRaw.core/raw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,16 +786,12 @@ static public int sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sql, out sql
return rc;
}

static public int sqlite3_prepare_v2(sqlite3 db, utf8z sql, out sqlite3_stmt stmt)
{
int rc = Provider.sqlite3_prepare_v2(db, sql, out var p, out var sp_tail);
stmt = sqlite3_stmt.From(p, db);
return rc;
}

static public int sqlite3_prepare_v2(sqlite3 db, string sql, out sqlite3_stmt stmt)
{
return sqlite3_prepare_v2(db, sql.to_utf8z(), out stmt);
var ba = sql.to_utf8_with_z();
var sp = new ReadOnlySpan<byte>(ba);
int rc = sqlite3_prepare_v2(db, sp, out stmt, out var sp_tail);
return rc;
}

static public int sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sql, out sqlite3_stmt stmt, out ReadOnlySpan<byte> tail)
Expand All @@ -806,13 +802,6 @@ static public int sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sql, out sql
return rc;
}

static public int sqlite3_prepare_v2(sqlite3 db, utf8z sql, out sqlite3_stmt stmt, out utf8z tail)
{
int rc = Provider.sqlite3_prepare_v2(db, sql, out var p, out tail);
stmt = sqlite3_stmt.From(p, db);
return rc;
}

static public int sqlite3_prepare_v2(sqlite3 db, string sql, out sqlite3_stmt stmt, out string tail)
{
var ba = sql.to_utf8_with_z();
Expand All @@ -829,26 +818,15 @@ static public int sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint fl
return rc;
}

static public int sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out sqlite3_stmt stmt)
{
int rc = Provider.sqlite3_prepare_v3(db, sql, flags, out var p, out var sp_tail);
stmt = sqlite3_stmt.From(p, db);
return rc;
}

static public int sqlite3_prepare_v3(sqlite3 db, string sql, uint flags, out sqlite3_stmt stmt)
{
return sqlite3_prepare_v3(db, sql.to_utf8z(), flags, out stmt);
}

static public int sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out sqlite3_stmt stmt, out ReadOnlySpan<byte> tail)
{
int rc = Provider.sqlite3_prepare_v3(db, sql, flags, out var p, out tail);
stmt = sqlite3_stmt.From(p, db);
var ba = sql.to_utf8_with_z();
var sp = new ReadOnlySpan<byte>(ba);
int rc = sqlite3_prepare_v3(db, sp, flags, out stmt, out var sp_tail);
return rc;
}

static public int sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out sqlite3_stmt stmt, out utf8z tail)
static public int sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out sqlite3_stmt stmt, out ReadOnlySpan<byte> tail)
{
int rc = Provider.sqlite3_prepare_v3(db, sql, flags, out var p, out tail);
stmt = sqlite3_stmt.From(p, db);
Expand All @@ -857,8 +835,10 @@ static public int sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out sqli

static public int sqlite3_prepare_v3(sqlite3 db, string sql, uint flags, out sqlite3_stmt stmt, out string tail)
{
int rc = sqlite3_prepare_v3(db, sql.to_utf8z(), flags, out stmt, out var sp_tail);
tail = sp_tail.utf8_to_string();
var ba = sql.to_utf8_with_z();
var sp = new ReadOnlySpan<byte>(ba);
int rc = sqlite3_prepare_v3(db, sp, flags, out stmt, out var sp_tail);
tail = utf8_span_to_string(sp_tail.Slice(0, sp_tail.Length - 1));
return rc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-x64\libe_sqlcipher.so" target="runtimes\linux-musl-x64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-x64\libe_sqlcipher.so" target="runtimes\alpine-x64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\mips64\libe_sqlcipher.so" target="runtimes\linux-mips64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\s390x\libe_sqlcipher.so" target="runtimes\linux-s390x\native\libe_sqlcipher.so" />
<file src=".\SQLitePCLRaw.lib.e_sqlcipher.targets" target="build\net461" />
<!--empty directory in lib to avoid nuget adding a reference-->
<file src="empty/" target="lib\net461" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>false</Pack>
</Content>
<Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\linux-s390x\native\libe_sqlcipher.so">
<Link>runtimes\linux-s390x\native\libe_sqlcipher.so</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>false</Pack>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<file src="$cb_bin_path$\e_sqlite3\linux\musl-x64\libe_sqlite3.so" target="runtimes\linux-musl-x64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\musl-x64\libe_sqlite3.so" target="runtimes\alpine-x64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\mips64\libe_sqlite3.so" target="runtimes\linux-mips64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\s390x\libe_sqlite3.so" target="runtimes\linux-s390x\native\libe_sqlite3.so" />
<file src=".\SQLitePCLRaw.lib.e_sqlite3.targets" target="build\net461" />
<!--empty directory in lib to avoid nuget adding a reference-->
<file src="empty/" target="lib\net461" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>false</Pack>
</Content>
<Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\linux-s390x\native\libe_sqlite3.so">
<Link>runtimes\linux-s390x\native\libe_sqlite3.so</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>false</Pack>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -308,17 +297,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -308,17 +297,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -299,17 +288,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -299,17 +288,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -302,17 +291,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -287,17 +276,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v2(sqlite3 db, utf8z sql, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v2(db, p_sql, -1, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sql, uint flags, out IntPtr stm, out ReadOnlySpan<byte> tail)
{
fixed (byte* p_sql = sql)
Expand All @@ -287,17 +276,6 @@ unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, ReadOnlySpan<byte> sq
}
}

unsafe int ISQLite3Provider.sqlite3_prepare_v3(sqlite3 db, utf8z sql, uint flags, out IntPtr stm, out utf8z tail)
{
fixed (byte* p_sql = sql)
{
var rc = NativeMethods.sqlite3_prepare_v3(db, p_sql, -1, flags, out stm, out var p_tail);
// TODO we could skip the strlen by using the length we were given
tail = utf8z.FromPtr(p_tail);
return rc;
}
}

int ISQLite3Provider.sqlite3_db_status(sqlite3 db, int op, out int current, out int highest, int resetFlg)
{
return NativeMethods.sqlite3_db_status(db, op, out current, out highest, resetFlg);
Expand Down
Loading

0 comments on commit 264be3a

Please sign in to comment.