Skip to content

Commit 1fb5d8a

Browse files
committed
fix UBSan for custom.c examples
1 parent 49234c0 commit 1fb5d8a

File tree

4 files changed

+62
-41
lines changed

4 files changed

+62
-41
lines changed

Doc/includes/newtypes/custom2.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ typedef struct {
1010
} CustomObject;
1111

1212
static void
13-
Custom_dealloc(CustomObject *self)
13+
Custom_dealloc(PyObject *op)
1414
{
15+
CustomObject *self = (CustomObject *)op;
1516
Py_XDECREF(self->first);
1617
Py_XDECREF(self->last);
17-
Py_TYPE(self)->tp_free((PyObject *) self);
18+
Py_TYPE(self)->tp_free(self);
1819
}
1920

2021
static PyObject *
@@ -39,8 +40,9 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3940
}
4041

4142
static int
42-
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
43+
Custom_init(PyObject *op, PyObject *args, PyObject *kwds)
4344
{
45+
CustomObject *self = (CustomObject *)op;
4446
static char *kwlist[] = {"first", "last", "number", NULL};
4547
PyObject *first = NULL, *last = NULL;
4648

@@ -69,8 +71,9 @@ static PyMemberDef Custom_members[] = {
6971
};
7072

7173
static PyObject *
72-
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
74+
Custom_name(PyObject *op, PyObject *Py_UNUSED(dummy))
7375
{
76+
CustomObject *self = (CustomObject *)op;
7477
if (self->first == NULL) {
7578
PyErr_SetString(PyExc_AttributeError, "first");
7679
return NULL;
@@ -83,7 +86,7 @@ Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
8386
}
8487

8588
static PyMethodDef Custom_methods[] = {
86-
{"name", (PyCFunction) Custom_name, METH_NOARGS,
89+
{"name", Custom_name, METH_NOARGS,
8790
"Return the name, combining the first and last name"
8891
},
8992
{NULL} /* Sentinel */
@@ -97,8 +100,8 @@ static PyTypeObject CustomType = {
97100
.tp_itemsize = 0,
98101
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
99102
.tp_new = Custom_new,
100-
.tp_init = (initproc) Custom_init,
101-
.tp_dealloc = (destructor) Custom_dealloc,
103+
.tp_init = Custom_init,
104+
.tp_dealloc = Custom_dealloc,
102105
.tp_members = Custom_members,
103106
.tp_methods = Custom_methods,
104107
};

Doc/includes/newtypes/custom3.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ typedef struct {
1010
} CustomObject;
1111

1212
static void
13-
Custom_dealloc(CustomObject *self)
13+
Custom_dealloc(PyObject *op)
1414
{
15+
CustomObject *self = (CustomObject *)op;
1516
Py_XDECREF(self->first);
1617
Py_XDECREF(self->last);
17-
Py_TYPE(self)->tp_free((PyObject *) self);
18+
Py_TYPE(self)->tp_free(self);
1819
}
1920

2021
static PyObject *
@@ -39,8 +40,9 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3940
}
4041

4142
static int
42-
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
43+
Custom_init(PyObject *op, PyObject *args, PyObject *kwds)
4344
{
45+
CustomObject *self = (CustomObject *)op;
4446
static char *kwlist[] = {"first", "last", "number", NULL};
4547
PyObject *first = NULL, *last = NULL;
4648

@@ -65,14 +67,16 @@ static PyMemberDef Custom_members[] = {
6567
};
6668

6769
static PyObject *
68-
Custom_getfirst(CustomObject *self, void *closure)
70+
Custom_getfirst(PyObject *op, void *closure)
6971
{
72+
CustomObject *self = (CustomObject *)op;
7073
return Py_NewRef(self->first);
7174
}
7275

7376
static int
74-
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
77+
Custom_setfirst(PyObject *op, PyObject *value, void *closure)
7578
{
79+
CustomObject *self = (CustomObject *)op;
7680
if (value == NULL) {
7781
PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
7882
return -1;
@@ -87,14 +91,16 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
8791
}
8892

8993
static PyObject *
90-
Custom_getlast(CustomObject *self, void *closure)
94+
Custom_getlast(PyObject *op, void *closure)
9195
{
96+
CustomObject *self = (CustomObject *)op;
9297
return Py_NewRef(self->last);
9398
}
9499

95100
static int
96-
Custom_setlast(CustomObject *self, PyObject *value, void *closure)
101+
Custom_setlast(PyObject *op, PyObject *value, void *closure)
97102
{
103+
CustomObject *self = (CustomObject *)op;
98104
if (value == NULL) {
99105
PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
100106
return -1;
@@ -109,21 +115,22 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure)
109115
}
110116

111117
static PyGetSetDef Custom_getsetters[] = {
112-
{"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
118+
{"first", Custom_getfirst, Custom_setfirst,
113119
"first name", NULL},
114-
{"last", (getter) Custom_getlast, (setter) Custom_setlast,
120+
{"last", Custom_getlast, Custom_setlast,
115121
"last name", NULL},
116122
{NULL} /* Sentinel */
117123
};
118124

119125
static PyObject *
120-
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
126+
Custom_name(PyObject *op, PyObject *Py_UNUSED(dummy))
121127
{
128+
CustomObject *self = (CustomObject *)op;
122129
return PyUnicode_FromFormat("%S %S", self->first, self->last);
123130
}
124131

125132
static PyMethodDef Custom_methods[] = {
126-
{"name", (PyCFunction) Custom_name, METH_NOARGS,
133+
{"name", Custom_name, METH_NOARGS,
127134
"Return the name, combining the first and last name"
128135
},
129136
{NULL} /* Sentinel */
@@ -137,8 +144,8 @@ static PyTypeObject CustomType = {
137144
.tp_itemsize = 0,
138145
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
139146
.tp_new = Custom_new,
140-
.tp_init = (initproc) Custom_init,
141-
.tp_dealloc = (destructor) Custom_dealloc,
147+
.tp_init = Custom_init,
148+
.tp_dealloc = Custom_dealloc,
142149
.tp_members = Custom_members,
143150
.tp_methods = Custom_methods,
144151
.tp_getset = Custom_getsetters,

Doc/includes/newtypes/custom4.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,30 @@ typedef struct {
1010
} CustomObject;
1111

1212
static int
13-
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
13+
Custom_traverse(PyObject *op, visitproc visit, void *arg)
1414
{
15+
CustomObject *self = (CustomObject *)op;
1516
Py_VISIT(self->first);
1617
Py_VISIT(self->last);
1718
return 0;
1819
}
1920

2021
static int
21-
Custom_clear(CustomObject *self)
22+
Custom_clear(PyObject *op)
2223
{
24+
CustomObject *self = (CustomObject *)op;
2325
Py_CLEAR(self->first);
2426
Py_CLEAR(self->last);
2527
return 0;
2628
}
2729

2830
static void
29-
Custom_dealloc(CustomObject *self)
31+
Custom_dealloc(PyObject *op)
3032
{
33+
CustomObject *self = (CustomObject *)op;
3134
PyObject_GC_UnTrack(self);
3235
Custom_clear(self);
33-
Py_TYPE(self)->tp_free((PyObject *) self);
36+
Py_TYPE(self)->tp_free(self);
3437
}
3538

3639
static PyObject *
@@ -55,8 +58,9 @@ Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5558
}
5659

5760
static int
58-
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
61+
Custom_init(PyObject *op, PyObject *args, PyObject *kwds)
5962
{
63+
CustomObject *self = (CustomObject *)op;
6064
static char *kwlist[] = {"first", "last", "number", NULL};
6165
PyObject *first = NULL, *last = NULL;
6266

@@ -81,14 +85,16 @@ static PyMemberDef Custom_members[] = {
8185
};
8286

8387
static PyObject *
84-
Custom_getfirst(CustomObject *self, void *closure)
88+
Custom_getfirst(PyObject *op, void *closure)
8589
{
90+
CustomObject *self = (CustomObject *)op;
8691
return Py_NewRef(self->first);
8792
}
8893

8994
static int
90-
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
95+
Custom_setfirst(PyObject *op, PyObject *value, void *closure)
9196
{
97+
CustomObject *self = (CustomObject *)op;
9298
if (value == NULL) {
9399
PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
94100
return -1;
@@ -103,14 +109,16 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
103109
}
104110

105111
static PyObject *
106-
Custom_getlast(CustomObject *self, void *closure)
112+
Custom_getlast(PyObject *op, void *closure)
107113
{
114+
CustomObject *self = (CustomObject *)op;
108115
return Py_NewRef(self->last);
109116
}
110117

111118
static int
112-
Custom_setlast(CustomObject *self, PyObject *value, void *closure)
119+
Custom_setlast(PyObject *op, PyObject *value, void *closure)
113120
{
121+
CustomObject *self = (CustomObject *)op;
114122
if (value == NULL) {
115123
PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
116124
return -1;
@@ -125,21 +133,22 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure)
125133
}
126134

127135
static PyGetSetDef Custom_getsetters[] = {
128-
{"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
136+
{"first", Custom_getfirst, Custom_setfirst,
129137
"first name", NULL},
130-
{"last", (getter) Custom_getlast, (setter) Custom_setlast,
138+
{"last", Custom_getlast, Custom_setlast,
131139
"last name", NULL},
132140
{NULL} /* Sentinel */
133141
};
134142

135143
static PyObject *
136-
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
144+
Custom_name(PyObject *op, PyObject *Py_UNUSED(dummy))
137145
{
146+
CustomObject *self = (CustomObject *)op;
138147
return PyUnicode_FromFormat("%S %S", self->first, self->last);
139148
}
140149

141150
static PyMethodDef Custom_methods[] = {
142-
{"name", (PyCFunction) Custom_name, METH_NOARGS,
151+
{"name", Custom_name, METH_NOARGS,
143152
"Return the name, combining the first and last name"
144153
},
145154
{NULL} /* Sentinel */
@@ -153,10 +162,10 @@ static PyTypeObject CustomType = {
153162
.tp_itemsize = 0,
154163
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
155164
.tp_new = Custom_new,
156-
.tp_init = (initproc) Custom_init,
157-
.tp_dealloc = (destructor) Custom_dealloc,
158-
.tp_traverse = (traverseproc) Custom_traverse,
159-
.tp_clear = (inquiry) Custom_clear,
165+
.tp_init = Custom_init,
166+
.tp_dealloc = Custom_dealloc,
167+
.tp_traverse = Custom_traverse,
168+
.tp_clear = Custom_clear,
160169
.tp_members = Custom_members,
161170
.tp_methods = Custom_methods,
162171
.tp_getset = Custom_getsetters,

Doc/includes/newtypes/sublist.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ typedef struct {
77
} SubListObject;
88

99
static PyObject *
10-
SubList_increment(SubListObject *self, PyObject *unused)
10+
SubList_increment(PyObject *op, PyObject *Py_UNUSED(dummy))
1111
{
12+
SubListObject *self = (SubListObject *)op;
1213
self->state++;
1314
return PyLong_FromLong(self->state);
1415
}
1516

1617
static PyMethodDef SubList_methods[] = {
17-
{"increment", (PyCFunction) SubList_increment, METH_NOARGS,
18+
{"increment", SubList_increment, METH_NOARGS,
1819
PyDoc_STR("increment state counter")},
1920
{NULL},
2021
};
2122

2223
static int
23-
SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
24+
SubList_init(PyObject *op, PyObject *args, PyObject *kwds)
2425
{
26+
SubListObject *self = (SubListObject *)op;
2527
if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0)
2628
return -1;
2729
self->state = 0;
@@ -35,7 +37,7 @@ static PyTypeObject SubListType = {
3537
.tp_basicsize = sizeof(SubListObject),
3638
.tp_itemsize = 0,
3739
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
38-
.tp_init = (initproc) SubList_init,
40+
.tp_init = SubList_init,
3941
.tp_methods = SubList_methods,
4042
};
4143

0 commit comments

Comments
 (0)