From 02a8047658ed55fdfa22dac7d1515d3b52e21bb4 Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Tue, 4 Nov 2025 11:55:50 +0100 Subject: [PATCH] Fix 0-byte reads/writes on blobs --- src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs b/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs index 641e05dac57..4e9cbc25a7d 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs @@ -206,8 +206,12 @@ public virtual int Read(Span buffer) count = (int)(Length - position); } - var rc = sqlite3_blob_read(_blob, buffer.Slice(0, count), (int)position); - SqliteException.ThrowExceptionForRC(rc, _connection.Handle); + // Newer sqlite3_blob_read returns error for 0-byte reads. + if (count > 0) + { + var rc = sqlite3_blob_read(_blob, buffer.Slice(0, count), (int)position); + SqliteException.ThrowExceptionForRC(rc, _connection.Handle); + } _position += count; return count; } @@ -280,8 +284,12 @@ public virtual void Write(ReadOnlySpan buffer) throw new NotSupportedException(Resources.ResizeNotSupported); } - var rc = sqlite3_blob_write(_blob, buffer.Slice(0, count), (int)position); - SqliteException.ThrowExceptionForRC(rc, _connection.Handle); + // Newer sqlite3_blob_write returns error for 0-byte writes. + if (count > 0) + { + var rc = sqlite3_blob_write(_blob, buffer.Slice(0, count), (int)position); + SqliteException.ThrowExceptionForRC(rc, _connection.Handle); + } _position += count; }