FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-155702: Fix sqlite3.Blob slice assignment with a step (GH-155703) by miss-islington · Pull Request #155765 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,18 @@ def test_blob_set_slice(self):
actual = self.cx.execute("select b from test").fetchone()[0]
self.assertEqual(actual, expected)

def test_blob_set_slice_with_step_keeps_bytes_intact(self):
# The buffer used for the read-patch-write cycle must not be the
# bytes object read from the blob: for a single byte it is an
# immortal singleton.
old_byte = self.data[5]
self.blob[5:6:2] = b"\xab"
self.assertEqual(bytes([old_byte])[0], old_byte)
self.assertEqual(self.blob[5:6], b"\xab")
expected = self.data[:5] + b"\xab" + self.data[6:]
actual = self.cx.execute("select b from test").fetchone()[0]
self.assertEqual(actual, expected)

def test_blob_set_empty_slice(self):
self.blob[0:0] = b""
self.assertEqual(self.blob[:], self.data)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :class:`sqlite3.Blob` slice assignment with a step.
It patched the bytes object read from the blob,
which for a single byte is an immortal singleton,
so that the value of that byte was changed in the whole process.
57 changes: 40 additions & 17 deletions Modules/_sqlite/blob.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,35 @@ read_single(pysqlite_Blob *self, Py_ssize_t offset)
return PyLong_FromUnsignedLong((unsigned long)buf);
}

static PyObject *
read_multiple(pysqlite_Blob *self, Py_ssize_t length, Py_ssize_t offset)
static int
inner_read(pysqlite_Blob *self, char *buf, Py_ssize_t length,
Py_ssize_t offset)
{
assert(length <= sqlite3_blob_bytes(self->blob));
assert(offset < sqlite3_blob_bytes(self->blob));

PyBytesWriter *writer = PyBytesWriter_Create(length);
if (writer == NULL) {
return NULL;
}
char *raw_buffer = PyBytesWriter_GetData(writer);

int rc;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_blob_read(self->blob, raw_buffer, (int)length, (int)offset);
rc = sqlite3_blob_read(self->blob, buf, (int)length, (int)offset);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
PyBytesWriter_Discard(writer);
blob_seterror(self, rc);
return -1;
}
return 0;
}

static PyObject *
read_multiple(pysqlite_Blob *self, Py_ssize_t length, Py_ssize_t offset)
{
PyBytesWriter *writer = PyBytesWriter_Create(length);
if (writer == NULL) {
return NULL;
}

if (inner_read(self, PyBytesWriter_GetData(writer), length, offset) < 0) {
PyBytesWriter_Discard(writer);
return NULL;
}
return PyBytesWriter_Finish(writer);
Expand Down Expand Up @@ -553,14 +562,28 @@ ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value)
rc = inner_write(self, vbuf.buf, len, start);
}
else {
PyObject *blob_bytes = read_multiple(self, stop - start, start);
if (blob_bytes != NULL) {
char *blob_buf = PyBytes_AS_STRING(blob_bytes);
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
blob_buf[j] = ((char *)vbuf.buf)[i];
/* Read the affected region, patch it and write it back. The
object returned by read_multiple() cannot be used as the buffer,
because for a single byte it is an immortal singleton. */
Py_ssize_t length = stop - start;
if (length <= 0) {
/* start > stop for a negative step; see gh-150449. */
PyErr_SetString(PyExc_ValueError, "size must be >= 0");
}
else {
char *buf = PyMem_Malloc(length);
if (buf == NULL) {
PyErr_NoMemory();
}
else {
if (inner_read(self, buf, length, start) == 0) {
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
buf[j] = ((char *)vbuf.buf)[i];
}
rc = inner_write(self, buf, length, start);
}
PyMem_Free(buf);
}
rc = inner_write(self, blob_buf, stop - start, start);
Py_DECREF(blob_bytes);
}
}
PyBuffer_Release(&vbuf);
Expand Down
Loading

Back | FazBrowse Home | New Git URL