Skip to content

Commit 801a41c

Browse files
committed
fix UBSan failures for PySSLContext
1 parent 80b9e79 commit 801a41c

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

Modules/_ssl.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ typedef struct {
312312
#endif
313313
} PySSLContext;
314314

315+
#define PySSLContext_CAST(op) ((PySSLContext *)(op))
316+
315317
typedef struct {
316318
int ssl; /* last seen error from SSL */
317319
int c; /* last seen error from libc */
@@ -3278,17 +3280,19 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
32783280
}
32793281

32803282
static int
3281-
context_traverse(PySSLContext *self, visitproc visit, void *arg)
3283+
context_traverse(PyObject *op, visitproc visit, void *arg)
32823284
{
3285+
PySSLContext *self = PySSLContext_CAST(op);
32833286
Py_VISIT(self->set_sni_cb);
32843287
Py_VISIT(self->msg_cb);
32853288
Py_VISIT(Py_TYPE(self));
32863289
return 0;
32873290
}
32883291

32893292
static int
3290-
context_clear(PySSLContext *self)
3293+
context_clear(PyObject *op)
32913294
{
3295+
PySSLContext *self = PySSLContext_CAST(op);
32923296
Py_CLEAR(self->set_sni_cb);
32933297
Py_CLEAR(self->msg_cb);
32943298
Py_CLEAR(self->keylog_filename);
@@ -3306,15 +3310,16 @@ context_clear(PySSLContext *self)
33063310
}
33073311

33083312
static void
3309-
context_dealloc(PySSLContext *self)
3313+
context_dealloc(PyObject *op)
33103314
{
3315+
PySSLContext *self = PySSLContext_CAST(op);
33113316
PyTypeObject *tp = Py_TYPE(self);
33123317
/* bpo-31095: UnTrack is needed before calling any callbacks */
33133318
PyObject_GC_UnTrack(self);
3314-
context_clear(self);
3319+
(void)context_clear(op);
33153320
SSL_CTX_free(self->ctx);
33163321
PyMem_FREE(self->alpn_protocols);
3317-
Py_TYPE(self)->tp_free(self);
3322+
tp->tp_free(self);
33183323
Py_DECREF(tp);
33193324
}
33203325

@@ -3908,7 +3913,9 @@ _ssl__SSLContext_check_hostname_set_impl(PySSLContext *self, PyObject *value)
39083913
}
39093914

39103915
static PyObject *
3911-
get_post_handshake_auth(PySSLContext *self, void *c) {
3916+
get_post_handshake_auth(PyObject *op, void *Py_UNUSED(closure))
3917+
{
3918+
PySSLContext *self = PySSLContext_CAST(op);
39123919
#if defined(PySSL_HAVE_POST_HS_AUTH)
39133920
return PyBool_FromLong(self->post_handshake_auth);
39143921
#else
@@ -3918,7 +3925,9 @@ get_post_handshake_auth(PySSLContext *self, void *c) {
39183925

39193926
#if defined(PySSL_HAVE_POST_HS_AUTH)
39203927
static int
3921-
set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) {
3928+
set_post_handshake_auth(PyObject *op, PyObject *arg, void *Py_UNUSED(closure))
3929+
{
3930+
PySSLContext *self = PySSLContext_CAST(op);
39223931
if (arg == NULL) {
39233932
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
39243933
return -1;
@@ -5197,18 +5206,18 @@ static PyGetSetDef context_getsetlist[] = {
51975206
_SSL__SSLCONTEXT__HOST_FLAGS_GETSETDEF
51985207
_SSL__SSLCONTEXT_MINIMUM_VERSION_GETSETDEF
51995208
_SSL__SSLCONTEXT_MAXIMUM_VERSION_GETSETDEF
5200-
{"keylog_filename", (getter) _PySSLContext_get_keylog_filename,
5201-
(setter) _PySSLContext_set_keylog_filename, NULL},
5202-
{"_msg_callback", (getter) _PySSLContext_get_msg_callback,
5203-
(setter) _PySSLContext_set_msg_callback, NULL},
5209+
{"keylog_filename", _PySSLContext_get_keylog_filename,
5210+
_PySSLContext_set_keylog_filename, NULL},
5211+
{"_msg_callback", _PySSLContext_get_msg_callback,
5212+
_PySSLContext_set_msg_callback, NULL},
52045213
_SSL__SSLCONTEXT_SNI_CALLBACK_GETSETDEF
52055214
#if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3)
52065215
_SSL__SSLCONTEXT_NUM_TICKETS_GETSETDEF
52075216
#endif
52085217
_SSL__SSLCONTEXT_OPTIONS_GETSETDEF
5209-
{"post_handshake_auth", (getter) get_post_handshake_auth,
5218+
{"post_handshake_auth", get_post_handshake_auth,
52105219
#if defined(PySSL_HAVE_POST_HS_AUTH)
5211-
(setter) set_post_handshake_auth,
5220+
set_post_handshake_auth,
52125221
#else
52135222
NULL,
52145223
#endif

Modules/_ssl/debughelpers.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ _PySSL_msg_callback(int write_p, int version, int content_type,
8585

8686

8787
static PyObject *
88-
_PySSLContext_get_msg_callback(PySSLContext *self, void *c) {
88+
_PySSLContext_get_msg_callback(PyObject *op, void *Py_UNUSED(closure))
89+
{
90+
PySSLContext *self = PySSLContext_CAST(op);
8991
if (self->msg_cb != NULL) {
9092
return Py_NewRef(self->msg_cb);
9193
} else {
@@ -94,7 +96,10 @@ _PySSLContext_get_msg_callback(PySSLContext *self, void *c) {
9496
}
9597

9698
static int
97-
_PySSLContext_set_msg_callback(PySSLContext *self, PyObject *arg, void *c) {
99+
_PySSLContext_set_msg_callback(PyObject *op, PyObject *arg,
100+
void *Py_UNUSED(closure))
101+
{
102+
PySSLContext *self = PySSLContext_CAST(op);
98103
Py_CLEAR(self->msg_cb);
99104
if (arg == Py_None) {
100105
SSL_CTX_set_msg_callback(self->ctx, NULL);
@@ -153,7 +158,9 @@ _PySSL_keylog_callback(const SSL *ssl, const char *line)
153158
}
154159

155160
static PyObject *
156-
_PySSLContext_get_keylog_filename(PySSLContext *self, void *c) {
161+
_PySSLContext_get_keylog_filename(PyObject *op, void *Py_UNUSED(closure))
162+
{
163+
PySSLContext *self = PySSLContext_CAST(op);
157164
if (self->keylog_filename != NULL) {
158165
return Py_NewRef(self->keylog_filename);
159166
} else {
@@ -162,7 +169,10 @@ _PySSLContext_get_keylog_filename(PySSLContext *self, void *c) {
162169
}
163170

164171
static int
165-
_PySSLContext_set_keylog_filename(PySSLContext *self, PyObject *arg, void *c) {
172+
_PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg,
173+
void *Py_UNUSED(closure))
174+
{
175+
PySSLContext *self = PySSLContext_CAST(op);
166176
FILE *fp;
167177
/* Reset variables and callback first */
168178
SSL_CTX_set_keylog_callback(self->ctx, NULL);

0 commit comments

Comments
 (0)