Skip to content

Commit

Permalink
make sz more robust in handling edge cases, null pointers #273
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsink committed Jun 26, 2019
1 parent 50d3782 commit a9aebc0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 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-2019 SourceGear, LLC</Copyright>
<Company>SourceGear</Company>
<Authors>Eric Sink</Authors>
<Version>2.0.0-pre20190626125110</Version>
<Version>2.0.0-pre20190626145402</Version>
<AssemblyVersion>2.0.0.541</AssemblyVersion>
<FileVersion>2.0.0.541</FileVersion>
<Description>SQLitePCLRaw is a Portable Class Library (PCL) for low-level (raw) access to SQLite</Description>
Expand Down
42 changes: 38 additions & 4 deletions src/SQLitePCLRaw.core/util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public ref readonly byte GetPinnableReference()

sz(ReadOnlySpan<byte> a)
{
if (a[a.Length - 1] != 0)
if (
(a.Length > 0)
&& (a[a.Length - 1] != 0)
)
{
throw new ArgumentException("zero terminated string required");
}
Expand All @@ -50,7 +53,14 @@ public ref readonly byte GetPinnableReference()

public static sz FromString(string s)
{
return new sz(s.to_utf8_with_z());
if (s == null)
{
return new sz(ReadOnlySpan<byte>.Empty);
}
else
{
return new sz(s.to_utf8_with_z());
}
}

unsafe static long my_strlen(byte* p)
Expand All @@ -76,16 +86,35 @@ unsafe static ReadOnlySpan<byte> to_span(IntPtr p)

unsafe public static sz FromPtr(byte* p)
{
return new sz(to_span(p));
if (p == null)
{
return new sz(ReadOnlySpan<byte>.Empty);
}
else
{
return new sz(to_span(p));
}
}

public static sz FromIntPtr(IntPtr p)
{
return new sz(to_span(p));
if (p == IntPtr.Zero)
{
return new sz(ReadOnlySpan<byte>.Empty);
}
else
{
return new sz(to_span(p));
}
}

public override string ToString()
{
if (sp.Length == 0)
{
return null;
}

unsafe
{
fixed (byte* q = sp)
Expand All @@ -98,6 +127,11 @@ public override string ToString()

static class util
{
public static sz to_sz(this string s)
{
return sz.FromString(s);
}

public static byte[] to_utf8_with_z(this string sourceText)
{
if (sourceText == null)
Expand Down

0 comments on commit a9aebc0

Please sign in to comment.