Skip to content

Commit 280a89f

Browse files
committed
fix UBSan failures for pysqlite_Cursor
1 parent bc51984 commit 280a89f

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Modules/_sqlite/cursor.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ stmt_reset(pysqlite_Statement *self)
146146
}
147147

148148
static int
149-
cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
149+
cursor_traverse(PyObject *op, visitproc visit, void *arg)
150150
{
151+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
151152
Py_VISIT(Py_TYPE(self));
152153
Py_VISIT(self->connection);
153154
Py_VISIT(self->description);
@@ -159,8 +160,9 @@ cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
159160
}
160161

161162
static int
162-
cursor_clear(pysqlite_Cursor *self)
163+
cursor_clear(PyObject *op)
163164
{
165+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
164166
Py_CLEAR(self->connection);
165167
Py_CLEAR(self->description);
166168
Py_CLEAR(self->row_cast_map);
@@ -176,14 +178,15 @@ cursor_clear(pysqlite_Cursor *self)
176178
}
177179

178180
static void
179-
cursor_dealloc(pysqlite_Cursor *self)
181+
cursor_dealloc(PyObject *op)
180182
{
183+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
181184
PyTypeObject *tp = Py_TYPE(self);
182185
PyObject_GC_UnTrack(self);
183186
if (self->in_weakreflist != NULL) {
184-
PyObject_ClearWeakRefs((PyObject*)self);
187+
PyObject_ClearWeakRefs(op);
185188
}
186-
tp->tp_clear((PyObject *)self);
189+
(void)tp->tp_clear(op);
187190
tp->tp_free(self);
188191
Py_DECREF(tp);
189192
}
@@ -1087,8 +1090,9 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
10871090
}
10881091

10891092
static PyObject *
1090-
pysqlite_cursor_iternext(pysqlite_Cursor *self)
1093+
pysqlite_cursor_iternext(PyObject *op)
10911094
{
1095+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
10921096
if (!check_cursor(self)) {
10931097
return NULL;
10941098
}
@@ -1125,7 +1129,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
11251129
}
11261130
if (!Py_IsNone(self->row_factory)) {
11271131
PyObject *factory = self->row_factory;
1128-
PyObject *args[] = { (PyObject *)self, row, };
1132+
PyObject *args[] = { op, row, };
11291133
PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL);
11301134
Py_SETREF(row, new_row);
11311135
}
@@ -1144,7 +1148,7 @@ pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)
11441148
{
11451149
PyObject* row;
11461150

1147-
row = pysqlite_cursor_iternext(self);
1151+
row = pysqlite_cursor_iternext((PyObject *)self);
11481152
if (!row && !PyErr_Occurred()) {
11491153
Py_RETURN_NONE;
11501154
}
@@ -1174,7 +1178,7 @@ pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
11741178
return NULL;
11751179
}
11761180

1177-
while ((row = pysqlite_cursor_iternext(self))) {
1181+
while ((row = pysqlite_cursor_iternext((PyObject *)self))) {
11781182
if (PyList_Append(list, row) < 0) {
11791183
Py_DECREF(row);
11801184
break;
@@ -1212,7 +1216,7 @@ pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
12121216
return NULL;
12131217
}
12141218

1215-
while ((row = pysqlite_cursor_iternext(self))) {
1219+
while ((row = pysqlite_cursor_iternext((PyObject *)self))) {
12161220
if (PyList_Append(list, row) < 0) {
12171221
Py_DECREF(row);
12181222
break;

Modules/_sqlite/cursor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ typedef struct
4747
PyObject* in_weakreflist; /* List of weak references */
4848
} pysqlite_Cursor;
4949

50+
#define _pysqlite_Cursor_CAST(op) ((pysqlite_Cursor *)(op))
51+
5052
int pysqlite_cursor_setup_types(PyObject *module);
5153

5254
#endif

0 commit comments

Comments
 (0)