Skip to content

Commit 187f76d

Browse files
authored
[3.8] bpo-40052: Fix alignment issue in PyVectorcall_Function() (GH-23999) (GH-24120)
Co-Authored-By: Andreas Schneider <[email protected]> Co-Authored-By: Antoine Pitrou <[email protected]>. Co-authored-by: Petr Viktorin <[email protected]> (cherry picked from commit 056c082) https://bugs.python.org/issue40052
1 parent 5ded7ef commit 187f76d

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Include/cpython/abstract.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ _PyVectorcall_Function(PyObject *callable)
8282
{
8383
PyTypeObject *tp = Py_TYPE(callable);
8484
Py_ssize_t offset = tp->tp_vectorcall_offset;
85-
vectorcallfunc *ptr;
85+
vectorcallfunc ptr;
8686
if (!PyType_HasFeature(tp, _Py_TPFLAGS_HAVE_VECTORCALL)) {
8787
return NULL;
8888
}
8989
assert(PyCallable_Check(callable));
9090
assert(offset > 0);
91-
ptr = (vectorcallfunc*)(((char *)callable) + offset);
92-
return *ptr;
91+
memcpy(&ptr, (char *) callable + offset, sizeof(ptr));
92+
return ptr;
9393
}
9494

9595
/* Call the callable object 'callable' with the "vectorcall" calling
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an alignment build warning/error in function ``PyVectorcall_Function()``.
2+
Patch by Andreas Schneider, Antoine Pitrou and Petr Viktorin.

Objects/call.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,14 @@ PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
175175
{
176176
/* get vectorcallfunc as in _PyVectorcall_Function, but without
177177
* the _Py_TPFLAGS_HAVE_VECTORCALL check */
178+
vectorcallfunc func;
178179
Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
179180
if (offset <= 0) {
180181
PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
181182
Py_TYPE(callable)->tp_name);
182183
return NULL;
183184
}
184-
vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
185+
memcpy(&func, (char *) callable + offset, sizeof(func));
185186
if (func == NULL) {
186187
PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
187188
Py_TYPE(callable)->tp_name);

0 commit comments

Comments
 (0)