8000 gh-111178: fix UBSan failures in `Modules/_sqlite` by picnixz · Pull Request #129087 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111178: fix UBSan failures in Modules/_sqlite #129087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Modules/_sqlite/blob.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "clinic/blob.c.h"
#undef clinic_state

#define _pysqlite_Blob_CAST(op) ((pysqlite_Blob *)(op))

/*[clinic input]
module _sqlite3
class _sqlite3.Blob "pysqlite_Blob *" "clinic_state()->BlobType"
Expand All @@ -29,32 +31,35 @@ close_blob(pysqlite_Blob *self)
}

static int
blob_traverse(pysqlite_Blob *self, visitproc visit, void *arg)
blob_traverse(PyObject *op, visitproc visit, void *arg)
{
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->connection);
return 0;
}

static int
blob_clear(pysqlite_Blob *self)
blob_clear(PyObject *op)
{
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
Py_CLEAR(self->connection);
return 0;
}

static void
blob_dealloc(pysqlite_Blob *self)
blob_dealloc(PyObject *op)
{
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);

close_blob(self);

if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
PyObject_ClearWeakRefs(op);
}
tp->tp_clear((PyObject *)self);
(void)tp->tp_clear(op);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -373,8 +378,9 @@ blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val,
}

static Py_ssize_t
blob_length(pysqlite_Blob *self)
blob_length(PyObject *op)
{
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
if (!check_blob(self)) {
return -1;
}
Expand Down Expand Up @@ -449,8 +455,9 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
}

static PyObject *
blob_subscript(pysqlite_Blob *self, PyObject *item)
blob_subscript(PyObject *op, PyObject *item)
{
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
if (!check_blob(self)) {
return NULL;
}
Expand Down Expand Up @@ -546,8 +553,9 @@ ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value)
}

static int
blob_ass_subscript(pysqlite_Blob *self, PyObject *item, PyObject *value)
blob_ass_subscript(PyObject *op, PyObject *item, PyObject *value)
{
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
if (!check_blob(self)) {
return -1;
}
Expand Down
50 changes: 33 additions & 17 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ sqlite3_int64_converter(PyObject *obj, sqlite3_int64 *result)
#include "clinic/connection.c.h"
#undef clinic_state

#define _pysqlite_Connection_CAST(op) ((pysqlite_Connection *)(op))

/*[clinic input]
module _sqlite3
class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionType"
Expand Down Expand Up @@ -385,8 +387,9 @@ do { \
} while (0)

static int
connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
connection_traverse(PyObject *op, visitproc visit, void *arg)
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->statement_cache);
Py_VISIT(self->cursors);
Expand All @@ -410,8 +413,9 @@ clear_callback_context(callback_context *ctx)
}

static int
connection_clear(pysqlite_Connection *self)
connection_clear(PyObject *op)
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
Py_CLEAR(self->statement_cache);
Py_CLEAR(self->cursors);
Py_CLEAR(self->blobs);
Expand Down Expand Up @@ -518,7 +522,7 @@ connection_dealloc(PyObject *self)
}
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
tp->tp_clear(self);
(void)tp->tp_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -1711,8 +1715,10 @@ int pysqlite_check_thread(pysqlite_Connection* self)
return 1;
}

static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
static PyObject *
pysqlite_connection_get_isolation_level(PyObject *op, void *Py_UNUSED(closure))
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
if (!pysqlite_check_connection(self)) {
return NULL;
}
Expand All @@ -1722,16 +1728,20 @@ static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* se
Py_RETURN_NONE;
}

static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
static PyObject *
pysqlite_connection_get_total_changes(PyObject *op, void *Py_UNUSED(closure))
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
if (!pysqlite_check_connection(self)) {
return NULL;
}
return PyLong_FromLong(sqlite3_total_changes(self->db));
}

static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
static PyObject *
pysqlite_connection_get_in_transaction(PyObject *op, void *Py_UNUSED(closure))
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
if (!pysqlite_check_connection(self)) {
return NULL;
}
Expand All @@ -1742,8 +1752,11 @@ static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* sel
}

static int
pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
pysqlite_connection_set_isolation_level(PyObject *op,
PyObject *isolation_level,
void *Py_UNUSED(ignored))
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
if (isolation_level == NULL) {
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
return -1;
Expand All @@ -1766,11 +1779,11 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
}

static PyObject *
pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
PyObject *kwargs)
pysqlite_connection_call(PyObject *op, PyObject *args, PyObject *kwargs)
{
PyObject* sql;
pysqlite_Statement* statement;
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);

if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
Expand Down Expand Up @@ -2521,8 +2534,9 @@ getconfig_impl(pysqlite_Connection *self, int op)
}

static PyObject *
get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
get_autocommit(PyObject *op, void *Py_UNUSED(closure))
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
Expand All @@ -2536,8 +2550,9 @@ get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
}

static int
set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
set_autocommit(PyObject *op, PyObject *val, void *Py_UNUSED(closure))
{
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return -1;
}
Expand All @@ -2562,7 +2577,7 @@ set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
}

static PyObject *
get_sig(PyObject *self, void *Py_UNUSED(ctx))
get_sig(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure))
{
return PyUnicode_FromString("(sql, /)");
}
Expand All @@ -2572,11 +2587,12 @@ static const char connection_doc[] =
PyDoc_STR("SQLite database connection object.");

static PyGetSetDef connection_getset[] = {
{"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
{"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
{"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
{"autocommit", (getter)get_autocommit, (setter)set_autocommit},
{"__text_signature__", get_sig, (setter)0},
{"isolation_level", pysqlite_connection_get_isolation_level,
pysqlite_connection_set_isolation_level},
{"total_changes", pysqlite_connection_get_total_changes, NULL},
{"in_transaction", pysqlite_connection_get_in_transaction, NULL},
{"autocommit", get_autocommit, set_autocommit},
{"__text_signature__", get_sig, NULL},
{NULL}
};

Expand Down
26 changes: 16 additions & 10 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ typedef enum {
#include "clinic/cursor.c.h"
#undef clinic_state

#define _pysqlite_Cursor_CAST(op) ((pysqlite_Cursor *)(op))

static inline int
check_cursor_locked(pysqlite_Cursor *cur)
{
Expand Down Expand Up @@ -146,8 +148,9 @@ stmt_reset(pysqlite_Statement *self)
}

static int
cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
cursor_traverse(PyObject *op, visitproc visit, void *arg)
{
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->connection);
Py_VISIT(self->description);
Expand All @@ -159,8 +162,9 @@ cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
}

static int
cursor_clear(pysqlite_Cursor *self)
cursor_clear(PyObject *op)
{
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
Py_CLEAR(self->connection);
Py_CLEAR(self->description);
Py_CLEAR(self->row_cast_map);
Expand All @@ -176,14 +180,15 @@ cursor_clear(pysqlite_Cursor *self)
}

static void
cursor_dealloc(pysqlite_Cursor *self)
cursor_dealloc(PyObject *op)
{
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
PyObject_ClearWeakRefs(op);
}
tp->tp_clear((PyObject *)self);
(void)tp->tp_clear(op);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -1087,8 +1092,9 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
}

static PyObject *
pysqlite_cursor_iternext(pysqlite_Cursor *self)
pysqlite_cursor_iternext(PyObject *op)
{
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
if (!check_cursor(self)) {
return NULL;
}
Expand Down Expand Up @@ -1125,7 +1131,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
}
if (!Py_IsNone(self->row_factory)) {
PyObject *factory = self->row_factory;
PyObject *args[] = { (PyObject *)self, row, };
PyObject *args[] = { op, row, };
PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL);
Py_SETREF(row, new_row);
}
Expand All @@ -1144,7 +1150,7 @@ pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)
{
PyObject* row;

row = pysqlite_cursor_iternext(self);
row = pysqlite_cursor_iternext((PyObject *)self);
if (!row && !PyErr_Occurred()) {
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -1174,7 +1180,7 @@ pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
return NULL;
}

while ((row = pysqlite_cursor_iternext(self))) {
while ((row = pysqlite_cursor_iternext((PyObject *)self))) {
if (PyList_Append(list, row) < 0) {
Py_DECREF(row);
break;
Expand Down Expand Up @@ -1212,7 +1218,7 @@ pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
return NULL;
}

while ((row = pysqlite_cursor_iternext(self))) {
while ((row = pysqlite_cursor_iternext((PyObject *)self))) {
if (PyList_Append(list, row) < 0) {
Py_DECREF(row);
break;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ module_clear(PyObject *module)
static void
module_free(void *module)
{
module_clear((PyObject *)module);
(void)module_clear((PyObject *)module);
}

#define ADD_TYPE(module, type) \
Expand Down
5 changes: 2 additions & 3 deletions Modules/_sqlite/prepare_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include "prepare_protocol.h"

static int
pysqlite_prepare_protocol_init(pysqlite_PrepareProtocol *self, PyObject *args,
PyObject *kwargs)
pysqlite_prepare_protocol_init(PyObject *self, PyObject *args, PyObject *kwargs)
{
return 0;
}
Expand All @@ -38,7 +37,7 @@ pysqlite_prepare_protocol_traverse(PyObject *self, visitproc visit, void *arg)
}

static void
pysqlite_prepare_protocol_dealloc(pysqlite_PrepareProtocol *self)
pysqlite_prepare_protocol_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
Expand Down
Loading
Loading
0