Skip to content

Commit bc51984

Browse files
committed
fix UBSan failures for pysqlite_Connection
1 parent 3ff8de4 commit bc51984

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

Modules/_sqlite/connection.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,9 @@ do { \
385385
} while (0)
386386

387387
static int
388-
connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
388+
connection_traverse(PyObject *op, visitproc visit, void *arg)
389389
{
390+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
390391
Py_VISIT(Py_TYPE(self));
391392
Py_VISIT(self->statement_cache);
392393
Py_VISIT(self->cursors);
@@ -410,8 +411,9 @@ clear_callback_context(callback_context *ctx)
410411
}
411412

412413
static int
413-
connection_clear(pysqlite_Connection *self)
414+
connection_clear(PyObject *op)
414415
{
416+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
415417
Py_CLEAR(self->statement_cache);
416418
Py_CLEAR(self->cursors);
417419
Py_CLEAR(self->blobs);
@@ -518,7 +520,7 @@ connection_dealloc(PyObject *self)
518520
}
519521
PyTypeObject *tp = Py_TYPE(self);
520522
PyObject_GC_UnTrack(self);
521-
tp->tp_clear(self);
523+
(void)tp->tp_clear(self);
522524
tp->tp_free(self);
523525
Py_DECREF(tp);
524526
}
@@ -1711,8 +1713,10 @@ int pysqlite_check_thread(pysqlite_Connection* self)
17111713
return 1;
17121714
}
17131715

1714-
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
1716+
static PyObject *
1717+
pysqlite_connection_get_isolation_level(PyObject *op, void *Py_UNUSED(closure))
17151718
{
1719+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17161720
if (!pysqlite_check_connection(self)) {
17171721
return NULL;
17181722
}
@@ -1722,16 +1726,20 @@ static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* se
17221726
Py_RETURN_NONE;
17231727
}
17241728

1725-
static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
1729+
static PyObject *
1730+
pysqlite_connection_get_total_changes(PyObject *op, void *Py_UNUSED(closure))
17261731
{
1732+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17271733
if (!pysqlite_check_connection(self)) {
17281734
return NULL;
17291735
}
17301736
return PyLong_FromLong(sqlite3_total_changes(self->db));
17311737
}
17321738

1733-
static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1739+
static PyObject *
1740+
pysqlite_connection_get_in_transaction(PyObject *op, void *Py_UNUSED(closure))
17341741
{
1742+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17351743
if (!pysqlite_check_connection(self)) {
17361744
return NULL;
17371745
}
@@ -1742,8 +1750,11 @@ static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* sel
17421750
}
17431751

17441752
static int
1745-
pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
1753+
pysqlite_connection_set_isolation_level(PyObject *op,
1754+
PyObject *isolation_level,
1755+
void *Py_UNUSED(ignored))
17461756
{
1757+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17471758
if (isolation_level == NULL) {
17481759
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
17491760
return -1;
@@ -1766,11 +1777,11 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
17661777
}
17671778

17681779
static PyObject *
1769-
pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1770-
PyObject *kwargs)
1780+
pysqlite_connection_call(PyObject *op, PyObject *args, PyObject *kwargs)
17711781
{
17721782
PyObject* sql;
17731783
pysqlite_Statement* statement;
1784+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17741785

17751786
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
17761787
return NULL;
@@ -2521,8 +2532,9 @@ getconfig_impl(pysqlite_Connection *self, int op)
25212532
}
25222533

25232534
static PyObject *
2524-
get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
2535+
get_autocommit(PyObject *op, void *Py_UNUSED(closure))
25252536
{
2537+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
25262538
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
25272539
return NULL;
25282540
}
@@ -2536,8 +2548,9 @@ get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
25362548
}
25372549

25382550
static int
2539-
set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
2551+
set_autocommit(PyObject *op, PyObject *val, void *Py_UNUSED(closure))
25402552
{
2553+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
25412554
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
25422555
return -1;
25432556
}
@@ -2562,7 +2575,7 @@ set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
25622575
}
25632576

25642577
static PyObject *
2565-
get_sig(PyObject *self, void *Py_UNUSED(ctx))
2578+
get_sig(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure))
25662579
{
25672580
return PyUnicode_FromString("(sql, /)");
25682581
}
@@ -2572,11 +2585,12 @@ static const char connection_doc[] =
25722585
PyDoc_STR("SQLite database connection object.");
25732586

25742587
static PyGetSetDef connection_getset[] = {
2575-
{"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
2576-
{"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
2577-
{"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
2578-
{"autocommit", (getter)get_autocommit, (setter)set_autocommit},
2579-
{"__text_signature__", get_sig, (setter)0},
2588+
{"isolation_level", pysqlite_connection_get_isolation_level,
2589+
pysqlite_connection_set_isolation_level},
2590+
{"total_changes", pysqlite_connection_get_total_changes, NULL},
2591+
{"in_transaction", pysqlite_connection_get_in_transaction, NULL},
2592+
{"autocommit", get_autocommit, set_autocommit},
2593+
{"__text_signature__", get_sig, NULL},
25802594
{NULL}
25812595
};
25822596

Modules/_sqlite/connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ typedef struct
105105
PyObject* NotSupportedError;
106106
} pysqlite_Connection;
107107

108+
#define _pysqlite_Connection_CAST(op) ((pysqlite_Connection *)(op))
109+
108110
int pysqlite_check_thread(pysqlite_Connection* self);
109111
int pysqlite_check_connection(pysqlite_Connection* con);
110112

0 commit comments

Comments
 (0)