]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-142787: Handle empty sqlite3 blob slices (GH-142824) (#145298)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 27 Feb 2026 00:13:08 +0000 (01:13 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Feb 2026 00:13:08 +0000 (01:13 +0100)
(cherry picked from commit 06b0920f1292690a22ab2b271dfefe2c63cacf07)

Co-authored-by: A.Ibrahim <abdulrasheedibrahim47@gmail.com>
Lib/test/test_sqlite3/test_dbapi.py
Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst [new file with mode: 0644]
Modules/_sqlite/blob.c

index 7ad4136350c765e31ccfc389d76f1d3709075084..4d815bc3d596231e012fd8ec17f2eb6bb7dd2add 100644 (file)
@@ -1410,6 +1410,11 @@ class BlobTests(unittest.TestCase):
     def test_blob_get_empty_slice(self):
         self.assertEqual(self.blob[5:5], b"")
 
+    def test_blob_get_empty_slice_oob_indices(self):
+        self.cx.execute("insert into test(b) values (?)", (b"abc",))
+        with self.cx.blobopen("test", "b", 2) as blob:
+            self.assertEqual(blob[5:-5], b"")
+
     def test_blob_get_slice_negative_index(self):
         self.assertEqual(self.blob[5:-5], self.data[5:-5])
 
diff --git a/Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst b/Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst
new file mode 100644 (file)
index 0000000..e928bd2
--- /dev/null
@@ -0,0 +1,2 @@
+Fix assertion failure in :mod:`sqlite3` blob subscript when slicing with\r
+indices that result in an empty slice.
index 6ad3f9c0968313fc936a55fb74f9294a3cb727f6..4db1ac474ef25313d747321318a76f2812862f5e 100644 (file)
@@ -428,6 +428,10 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
         return NULL;
     }
 
+    if (len == 0) {
+        return PyBytes_FromStringAndSize(NULL, 0);
+    }
+
     if (step == 1) {
         return read_multiple(self, len, start);
     }