From 34b04d37500095d0b261366c764eff65c6916210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20amca?= Date: Sat, 14 Nov 2026 20:25:01 +0300 Subject: [PATCH] Throw ObjectDisposedException from SqliteBlob.Write(ReadOnlySpan) Read(Span) and Write(byte[], int, int) check whether the blob was disposed, but Write(ReadOnlySpan) didn't. Writing to a disposed blob through the span overload passed the null handle to sqlite3_blob_write and failed with "ArgumentNullException: SafeHandle cannot be null", and an empty write silently succeeded. Add the same disposed check to the span overload. --- src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs | 5 +++++ .../SqliteBlobTest.cs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs b/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs index 36d6aec734d..e79a4925fa0 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs @@ -254,6 +254,11 @@ public override void Write(byte[] buffer, int offset, int count) /// public override void Write(ReadOnlySpan buffer) { + if (_blob == null) + { + throw new ObjectDisposedException(objectName: null); + } + if (!CanWrite) { throw new NotSupportedException(Resources.WriteNotSupported); diff --git a/test/Microsoft.Data.Sqlite.Tests/SqliteBlobTest.cs b/test/Microsoft.Data.Sqlite.Tests/SqliteBlobTest.cs index eeb73c2cdf6..1ec90252174 100644 --- a/test/Microsoft.Data.Sqlite.Tests/SqliteBlobTest.cs +++ b/test/Microsoft.Data.Sqlite.Tests/SqliteBlobTest.cs @@ -339,6 +339,25 @@ public void Write_throws_when_disposed() var ex = Assert.Throws(() => stream.Write([3], 0, 1)); } + [Fact] + public void Write_span_throws_when_disposed() + { + var stream = CreateStream(); + stream.Dispose(); + + Assert.Throws(() => stream.Write(new ReadOnlySpan([3]))); + Assert.Throws(() => stream.Write(ReadOnlySpan.Empty)); + } + + [Fact] + public void Read_span_throws_when_disposed() + { + var stream = CreateStream(); + stream.Dispose(); + + Assert.Throws(() => stream.Read(new byte[1].AsSpan())); + } + [Fact] public void Empty_works() {