Skip to content

Commit f892de4

Browse files
committed
Fix 0-byte reads/writes on blobs
1 parent aeb51fa commit f892de4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@ public virtual int Read(Span<byte> buffer)
206206
count = (int)(Length - position);
207207
}
208208

209-
var rc = sqlite3_blob_read(_blob, buffer.Slice(0, count), (int)position);
210-
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
209+
// Newer sqlite3_blob_read returns error for 0-byte reads.
210+
if (count > 0)
211+
{
212+
var rc = sqlite3_blob_read(_blob, buffer.Slice(0, count), (int)position);
213+
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
214+
}
211215
_position += count;
212216
return count;
213217
}
@@ -280,8 +284,12 @@ public virtual void Write(ReadOnlySpan<byte> buffer)
280284
throw new NotSupportedException(Resources.ResizeNotSupported);
281285
}
282286

283-
var rc = sqlite3_blob_write(_blob, buffer.Slice(0, count), (int)position);
284-
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
287+
// Newer sqlite3_blob_write returns error for 0-byte writes.
288+
if (count > 0)
289+
{
290+
var rc = sqlite3_blob_write(_blob, buffer.Slice(0, count), (int)position);
291+
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
292+
}
285293
_position += count;
286294
}
287295

0 commit comments

Comments
 (0)