Skip to content

Commit 881984b

Browse files
authored
gh-111178: fix UBSan failures in Modules/_sqlite (GH-129087)
* fix UBSan failures for `pysqlite_Blob` * fix UBSan failures for `pysqlite_Connection` * fix UBSan failures for `pysqlite_Cursor` * fix UBSan failures for `pysqlite_PrepareProtocol` * fix UBSan failures for `pysqlite_Row` * fix UBSan failures for `pysqlite_Statement` * suppress unused return values
1 parent 9d63ae5 commit 881984b

File tree

7 files changed

+101
-55
lines changed

7 files changed

+101
-55
lines changed

Modules/_sqlite/blob.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "clinic/blob.c.h"
1010
#undef clinic_state
1111

12+
#define _pysqlite_Blob_CAST(op) ((pysqlite_Blob *)(op))
13+
1214
/*[clinic input]
1315
module _sqlite3
1416
class _sqlite3.Blob "pysqlite_Blob *" "clinic_state()->BlobType"
@@ -29,32 +31,35 @@ close_blob(pysqlite_Blob *self)
2931
}
3032

3133
static int
32-
blob_traverse(pysqlite_Blob *self, visitproc visit, void *arg)
34+
blob_traverse(PyObject *op, visitproc visit, void *arg)
3335
{
36+
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
3437
Py_VISIT(Py_TYPE(self));
3538
Py_VISIT(self->connection);
3639
return 0;
3740
}
3841

3942
static int
40-
blob_clear(pysqlite_Blob *self)
43+
blob_clear(PyObject *op)
4144
{
45+
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
4246
Py_CLEAR(self->connection);
4347
return 0;
4448
}
4549

4650
static void
47-
blob_dealloc(pysqlite_Blob *self)
51+
blob_dealloc(PyObject *op)
4852
{
53+
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
4954
PyTypeObject *tp = Py_TYPE(self);
5055
PyObject_GC_UnTrack(self);
5156

5257
close_blob(self);
5358

5459
if (self->in_weakreflist != NULL) {
55-
PyObject_ClearWeakRefs((PyObject*)self);
60+
PyObject_ClearWeakRefs(op);
5661
}
57-
tp->tp_clear((PyObject *)self);
62+
(void)tp->tp_clear(op);
5863
tp->tp_free(self);
5964
Py_DECREF(tp);
6065
}
@@ -373,8 +378,9 @@ blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val,
373378
}
374379

375380
static Py_ssize_t
376-
blob_length(pysqlite_Blob *self)
381+
blob_length(PyObject *op)
377382
{
383+
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
378384
if (!check_blob(self)) {
379385
return -1;
380386
}
@@ -449,8 +455,9 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
449455
}
450456

451457
static PyObject *
452-
blob_subscript(pysqlite_Blob *self, PyObject *item)
458+
blob_subscript(PyObject *op, PyObject *item)
453459
{
460+
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
454461
if (!check_blob(self)) {
455462
return NULL;
456463
}
@@ -546,8 +553,9 @@ ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value)
546553
}
547554

548555
static int
549-
blob_ass_subscript(pysqlite_Blob *self, PyObject *item, PyObject *value)
556+
blob_ass_subscript(PyObject *op, PyObject *item, PyObject *value)
550557
{
558+
pysqlite_Blob *self = _pysqlite_Blob_CAST(op);
551559
if (!check_blob(self)) {
552560
return -1;
553561
}

Modules/_sqlite/connection.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ sqlite3_int64_converter(PyObject *obj, sqlite3_int64 *result)
135135
#include "clinic/connection.c.h"
136136
#undef clinic_state
137137

138+
#define _pysqlite_Connection_CAST(op) ((pysqlite_Connection *)(op))
139+
138140
/*[clinic input]
139141
module _sqlite3
140142
class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionType"
@@ -384,8 +386,9 @@ do { \
384386
} while (0)
385387

386388
static int
387-
connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
389+
connection_traverse(PyObject *op, visitproc visit, void *arg)
388390
{
391+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
389392
Py_VISIT(Py_TYPE(self));
390393
Py_VISIT(self->statement_cache);
391394
Py_VISIT(self->cursors);
@@ -409,8 +412,9 @@ clear_callback_context(callback_context *ctx)
409412
}
410413

411414
static int
412-
connection_clear(pysqlite_Connection *self)
415+
connection_clear(PyObject *op)
413416
{
417+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
414418
Py_CLEAR(self->statement_cache);
415419
Py_CLEAR(self->cursors);
416420
Py_CLEAR(self->blobs);
@@ -517,7 +521,7 @@ connection_dealloc(PyObject *self)
517521
}
518522
PyTypeObject *tp = Py_TYPE(self);
519523
PyObject_GC_UnTrack(self);
520-
tp->tp_clear(self);
524+
(void)tp->tp_clear(self);
521525
tp->tp_free(self);
522526
Py_DECREF(tp);
523527
}
@@ -1715,8 +1719,10 @@ int pysqlite_check_thread(pysqlite_Connection* self)
17151719
return 1;
17161720
}
17171721

1718-
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
1722+
static PyObject *
1723+
pysqlite_connection_get_isolation_level(PyObject *op, void *Py_UNUSED(closure))
17191724
{
1725+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17201726
if (!pysqlite_check_connection(self)) {
17211727
return NULL;
17221728
}
@@ -1726,16 +1732,20 @@ static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* se
17261732
Py_RETURN_NONE;
17271733
}
17281734

1729-
static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
1735+
static PyObject *
1736+
pysqlite_connection_get_total_changes(PyObject *op, void *Py_UNUSED(closure))
17301737
{
1738+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17311739
if (!pysqlite_check_connection(self)) {
17321740
return NULL;
17331741
}
17341742
return PyLong_FromLong(sqlite3_total_changes(self->db));
17351743
}
17361744

1737-
static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1745+
static PyObject *
1746+
pysqlite_connection_get_in_transaction(PyObject *op, void *Py_UNUSED(closure))
17381747
{
1748+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17391749
if (!pysqlite_check_connection(self)) {
17401750
return NULL;
17411751
}
@@ -1746,8 +1756,11 @@ static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* sel
17461756
}
17471757

17481758
static int
1749-
pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
1759+
pysqlite_connection_set_isolation_level(PyObject *op,
1760+
PyObject *isolation_level,
1761+
void *Py_UNUSED(ignored))
17501762
{
1763+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17511764
if (isolation_level == NULL) {
17521765
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
17531766
return -1;
@@ -1770,11 +1783,11 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
17701783
}
17711784

17721785
static PyObject *
1773-
pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1774-
PyObject *kwargs)
1786+
pysqlite_connection_call(PyObject *op, PyObject *args, PyObject *kwargs)
17751787
{
17761788
PyObject* sql;
17771789
pysqlite_Statement* statement;
1790+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
17781791

17791792
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
17801793
return NULL;
@@ -2525,8 +2538,9 @@ getconfig_impl(pysqlite_Connection *self, int op)
25252538
}
25262539

25272540
static PyObject *
2528-
get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
2541+
get_autocommit(PyObject *op, void *Py_UNUSED(closure))
25292542
{
2543+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
25302544
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
25312545
return NULL;
25322546
}
@@ -2540,8 +2554,9 @@ get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
25402554
}
25412555

25422556
static int
2543-
set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
2557+
set_autocommit(PyObject *op, PyObject *val, void *Py_UNUSED(closure))
25442558
{
2559+
pysqlite_Connection *self = _pysqlite_Connection_CAST(op);
25452560
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
25462561
return -1;
25472562
}
@@ -2566,7 +2581,7 @@ set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
25662581
}
25672582

25682583
static PyObject *
2569-
get_sig(PyObject *self, void *Py_UNUSED(ctx))
2584+
get_sig(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure))
25702585
{
25712586
return PyUnicode_FromString("(sql, /)");
25722587
}
@@ -2576,11 +2591,12 @@ static const char connection_doc[] =
25762591
PyDoc_STR("SQLite database connection object.");
25772592

25782593
static PyGetSetDef connection_getset[] = {
2579-
{"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
2580-
{"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
2581-
{"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
2582-
{"autocommit", (getter)get_autocommit, (setter)set_autocommit},
2583-
{"__text_signature__", get_sig, (setter)0},
2594+
{"isolation_level", pysqlite_connection_get_isolation_level,
2595+
pysqlite_connection_set_isolation_level},
2596+
{"total_changes", pysqlite_connection_get_total_changes, NULL},
2597+
{"in_transaction", pysqlite_connection_get_in_transaction, NULL},
2598+
{"autocommit", get_autocommit, set_autocommit},
2599+
{"__text_signature__", get_sig, NULL},
25842600
{NULL}
25852601
};
25862602

Modules/_sqlite/cursor.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ typedef enum {
4444
#include "clinic/cursor.c.h"
4545
#undef clinic_state
4646

47+
#define _pysqlite_Cursor_CAST(op) ((pysqlite_Cursor *)(op))
48+
4749
static inline int
4850
check_cursor_locked(pysqlite_Cursor *cur)
4951
{
@@ -146,8 +148,9 @@ stmt_reset(pysqlite_Statement *self)
146148
}
147149

148150
static int
149-
cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
151+
cursor_traverse(PyObject *op, visitproc visit, void *arg)
150152
{
153+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
151154
Py_VISIT(Py_TYPE(self));
152155
Py_VISIT(self->connection);
153156
Py_VISIT(self->description);
@@ -159,8 +162,9 @@ cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
159162
}
160163

161164
static int
162-
cursor_clear(pysqlite_Cursor *self)
165+
cursor_clear(PyObject *op)
163166
{
167+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
164168
Py_CLEAR(self->connection);
165169
Py_CLEAR(self->description);
166170
Py_CLEAR(self->row_cast_map);
@@ -176,14 +180,15 @@ cursor_clear(pysqlite_Cursor *self)
176180
}
177181

178182
static void
179-
cursor_dealloc(pysqlite_Cursor *self)
183+
cursor_dealloc(PyObject *op)
180184
{
185+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
181186
PyTypeObject *tp = Py_TYPE(self);
182187
PyObject_GC_UnTrack(self);
183188
if (self->in_weakreflist != NULL) {
184-
PyObject_ClearWeakRefs((PyObject*)self);
189+
PyObject_ClearWeakRefs(op);
185190
}
186-
tp->tp_clear((PyObject *)self);
191+
(void)tp->tp_clear(op);
187192
tp->tp_free(self);
188193
Py_DECREF(tp);
189194
}
@@ -1087,8 +1092,9 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
10871092
}
10881093

10891094
static PyObject *
1090-
pysqlite_cursor_iternext(pysqlite_Cursor *self)
1095+
pysqlite_cursor_iternext(PyObject *op)
10911096
{
1097+
pysqlite_Cursor *self = _pysqlite_Cursor_CAST(op);
10921098
if (!check_cursor(self)) {
10931099
return NULL;
10941100
}
@@ -1125,7 +1131,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
11251131
}
11261132
if (!Py_IsNone(self->row_factory)) {
11271133
PyObject *factory = self->row_factory;
1128-
PyObject *args[] = { (PyObject *)self, row, };
1134+
PyObject *args[] = { op, row, };
11291135
PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL);
11301136
Py_SETREF(row, new_row);
11311137
}
@@ -1144,7 +1150,7 @@ pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)
11441150
{
11451151
PyObject* row;
11461152

1147-
row = pysqlite_cursor_iternext(self);
1153+
row = pysqlite_cursor_iternext((PyObject *)self);
11481154
if (!row && !PyErr_Occurred()) {
11491155
Py_RETURN_NONE;
11501156
}
@@ -1174,7 +1180,7 @@ pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
11741180
return NULL;
11751181
}
11761182

1177-
while ((row = pysqlite_cursor_iternext(self))) {
1183+
while ((row = pysqlite_cursor_iternext((PyObject *)self))) {
11781184
if (PyList_Append(list, row) < 0) {
11791185
Py_DECREF(row);
11801186
break;
@@ -1212,7 +1218,7 @@ pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
12121218
return NULL;
12131219
}
12141220

1215-
while ((row = pysqlite_cursor_iternext(self))) {
1221+
while ((row = pysqlite_cursor_iternext((PyObject *)self))) {
12161222
if (PyList_Append(list, row) < 0) {
12171223
Py_DECREF(row);
12181224
break;

Modules/_sqlite/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ module_clear(PyObject *module)
617617
static void
618618
module_free(void *module)
619619
{
620-
module_clear((PyObject *)module);
620+
(void)module_clear((PyObject *)module);
621621
}
622622

623623
#define ADD_TYPE(module, type) \

Modules/_sqlite/prepare_protocol.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
#include "prepare_protocol.h"
2525

2626
static int
27-
pysqlite_prepare_protocol_init(pysqlite_PrepareProtocol *self, PyObject *args,
28-
PyObject *kwargs)
27+
pysqlite_prepare_protocol_init(PyObject *self, PyObject *args, PyObject *kwargs)
2928
{
3029
return 0;
3130
}
@@ -38,7 +37,7 @@ pysqlite_prepare_protocol_traverse(PyObject *self, visitproc visit, void *arg)
3837
}
3938

4039
static void
41-
pysqlite_prepare_protocol_dealloc(pysqlite_PrepareProtocol *self)
40+
pysqlite_prepare_protocol_dealloc(PyObject *self)
4241
{
4342
PyTypeObject *tp = Py_TYPE(self);
4443
PyObject_GC_UnTrack(self);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy