Skip to content

Commit 62f4782

Browse files
[3.14] gh-150913: Fix sqlite3.Blob validation for empty slice assignment (GH-150915) (GH-150924)
ass_subscript_slice() returned early when the computed slice length was zero, bypassing validation performed for non-empty slices. (cherry picked from commit fc9c4db) Co-authored-by: Jiseok CHOI <jiseok.dev@gmail.com>
1 parent 2d3d11c commit 62f4782

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,18 @@ def test_blob_set_empty_slice(self):
14081408
self.blob[0:0] = b""
14091409
self.assertEqual(self.blob[:], self.data)
14101410

1411+
def test_blob_set_empty_slice_wrong_type(self):
1412+
with self.assertRaises(TypeError):
1413+
self.blob[5:5] = None
1414+
1415+
def test_blob_set_empty_slice_wrong_size(self):
1416+
with self.assertRaisesRegex(IndexError, "wrong size"):
1417+
self.blob[5:5] = b"123"
1418+
1419+
def test_blob_set_empty_slice_correct(self):
1420+
self.blob[5:5] = b""
1421+
self.assertEqual(self.blob[:], self.data)
1422+
14111423
def test_blob_set_slice_with_skip(self):
14121424
self.blob[0:10:2] = b"12345"
14131425
actual = self.cx.execute("select b from test").fetchone()[0]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :class:`sqlite3.Blob` slice assignment to raise
2+
:exc:`TypeError` and :exc:`IndexError` for type and size mismatches
3+
respectively, even when the target slice is empty.

Modules/_sqlite/blob.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,21 +524,25 @@ ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value)
524524
return -1;
525525
}
526526

527-
if (len == 0) {
528-
return 0;
529-
}
530-
531527
Py_buffer vbuf;
532528
if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) {
533529
return -1;
534530
}
535531

536-
int rc = -1;
537532
if (vbuf.len != len) {
538533
PyErr_SetString(PyExc_IndexError,
539534
"Blob slice assignment is wrong size");
535+
PyBuffer_Release(&vbuf);
536+
return -1;
540537
}
541-
else if (step == 1) {
538+
539+
if (len == 0) {
540+
PyBuffer_Release(&vbuf);
541+
return 0;
542+
}
543+
544+
int rc = -1;
545+
if (step == 1) {
542546
rc = inner_write(self, vbuf.buf, len, start);
543547
}
544548
else {

0 commit comments

Comments
 (0)