Skip to content

Commit 588092f

Browse files
gh-156101: Fix sqlite3 Cursor.arraysize on a failed assignment
PyLong_AsUInt32() stores 0 in the target on error, so the attribute was clobbered when the assigned value was too large.
1 parent e8158d1 commit 588092f

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ def test_invalid_array_size(self):
10781078
self.assertRaises(TypeError, setter, 1.0)
10791079
self.assertRaises(ValueError, setter, -3)
10801080
self.assertRaises(OverflowError, setter, UINT32_MAX + 1)
1081+
# a failed assignment does not change the value
1082+
self.assertEqual(self.cu.arraysize, 1)
10811083

10821084
def test_fetchmany(self):
10831085
# no active SQL statement
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :attr:`sqlite3.Cursor.arraysize` being set to 0 if the assigned value is
2+
too large.
3+
The attribute is now left unchanged if the assignment fails.

Modules/_sqlite/cursor.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,12 @@ static int
13781378
_sqlite3_Cursor_arraysize_set_impl(pysqlite_Cursor *self, PyObject *value)
13791379
/*[clinic end generated code: output=af59a6b09f8cce6e input=ace48cb114e26060]*/
13801380
{
1381-
return PyLong_AsUInt32(value, &self->arraysize);
1381+
uint32_t arraysize;
1382+
if (PyLong_AsUInt32(value, &arraysize) < 0) {
1383+
return -1;
1384+
}
1385+
self->arraysize = arraysize;
1386+
return 0;
13821387
}
13831388

13841389
static PyMethodDef cursor_methods[] = {

0 commit comments

Comments
 (0)