Skip to content

Commit dd99012

Browse files
committed
fix UBSan failures for pysqlite_Row
1 parent 3806f27 commit dd99012

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

Modules/_sqlite/row.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,18 @@ class _sqlite3.Row "pysqlite_Row *" "clinic_state()->RowType"
3939
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=966c53403d7f3a40]*/
4040

4141
static int
42-
row_clear(pysqlite_Row *self)
42+
row_clear(PyObject *op)
4343
{
44+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
4445
Py_CLEAR(self->data);
4546
Py_CLEAR(self->description);
4647
return 0;
4748
}
4849

4950
static int
50-
row_traverse(pysqlite_Row *self, visitproc visit, void *arg)
51+
row_traverse(PyObject *op, visitproc visit, void *arg)
5152
{
53+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
5254
Py_VISIT(Py_TYPE(self));
5355
Py_VISIT(self->data);
5456
Py_VISIT(self->description);
@@ -60,7 +62,7 @@ pysqlite_row_dealloc(PyObject *self)
6062
{
6163
PyTypeObject *tp = Py_TYPE(self);
6264
PyObject_GC_UnTrack(self);
63-
tp->tp_clear(self);
65+
(void)tp->tp_clear(self);
6466
tp->tp_free(self);
6567
Py_DECREF(tp);
6668
}
@@ -94,10 +96,12 @@ pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor,
9496
return (PyObject *) self;
9597
}
9698

97-
PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
99+
static PyObject *
100+
pysqlite_row_item(PyObject *op, Py_ssize_t idx)
98101
{
99-
PyObject *item = PyTuple_GetItem(self->data, idx);
100-
return Py_XNewRef(item);
102+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
103+
PyObject *item = PyTuple_GetItem(self->data, idx);
104+
return Py_XNewRef(item);
101105
}
102106

103107
static int
@@ -129,10 +133,11 @@ equal_ignore_case(PyObject *left, PyObject *right)
129133
}
130134

131135
static PyObject *
132-
pysqlite_row_subscript(pysqlite_Row *self, PyObject *idx)
136+
pysqlite_row_subscript(PyObject *op, PyObject *idx)
133137
{
134138
Py_ssize_t _idx;
135139
Py_ssize_t nitems, i;
140+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
136141

137142
if (PyLong_Check(idx)) {
138143
_idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
@@ -174,8 +179,9 @@ pysqlite_row_subscript(pysqlite_Row *self, PyObject *idx)
174179
}
175180

176181
static Py_ssize_t
177-
pysqlite_row_length(pysqlite_Row* self)
182+
pysqlite_row_length(PyObject *op)
178183
{
184+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
179185
return PyTuple_GET_SIZE(self->data);
180186
}
181187

@@ -208,24 +214,30 @@ pysqlite_row_keys_impl(pysqlite_Row *self)
208214
return list;
209215
}
210216

211-
static PyObject* pysqlite_iter(pysqlite_Row* self)
217+
static PyObject *
218+
pysqlite_iter(PyObject *op)
212219
{
220+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
213221
return PyObject_GetIter(self->data);
214222
}
215223

216-
static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
224+
static Py_hash_t
225+
pysqlite_row_hash(PyObject *op)
217226
{
227+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
218228
return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
219229
}
220230

221-
static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
231+
static PyObject *
232+
pysqlite_row_richcompare(PyObject *op, PyObject *opother, int opid)
222233
{
223234
if (opid != Py_EQ && opid != Py_NE)
224235
Py_RETURN_NOTIMPLEMENTED;
225236

237+
pysqlite_Row *self = _pysqlite_Row_CAST(op);
226238
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
227-
if (PyObject_TypeCheck(_other, state->RowType)) {
228-
pysqlite_Row *other = (pysqlite_Row *)_other;
239+
if (PyObject_TypeCheck(opother, state->RowType)) {
240+
pysqlite_Row *other = (pysqlite_Row *)opother;
229241
int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
230242
if (eq < 0) {
231243
return NULL;

Modules/_sqlite/row.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ typedef struct _Row
3232
PyObject* description;
3333
} pysqlite_Row;
3434

35+
#define _pysqlite_Row_CAST(op) ((pysqlite_Row *)(op))
36+
3537
int pysqlite_row_setup_types(PyObject *module);
3638

3739
#endif

0 commit comments

Comments
 (0)