[Python-checkins] [3.9] bpo-40052: Fix alignment issue in PyVectorcall_Function() (GH-23999) (GH-24005)

encukou webhook-mailer at python.org
Tue Jan 5 10:47:23 EST 2021


https://github.com/python/cpython/commit/6e72ab909de58e89b5a7cd6899587e203cf129a3
commit: 6e72ab909de58e89b5a7cd6899587e203cf129a3
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: encukou <encukou at gmail.com>
date: 2021-01-05T16:46:58+01:00
summary:

[3.9] bpo-40052: Fix alignment issue in PyVectorcall_Function() (GH-23999) (GH-24005)

```
In file included from /usr/include/python3.8/Python.h:147:
In file included from /usr/include/python3.8/abstract.h:837:
/usr/include/python3.8/cpython/abstract.h:91:11: error: cast from 'char *' to 'vectorcallfunc *'
(aka 'struct _object *(**)(struct _object *, struct _object *const *, unsigned long, struct _object *)')
increases required alignment from 1 to 8 [-Werror,-Wcast-align]

    ptr = (vectorcallfunc*)(((char *)callable) + offset);
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
```
Co-authored-by: Petr Viktorin <encukou at gmail.com>
Co-Authored-By: Andreas Schneider <asn at cryptomilk.org>
Co-Authored-By: Antoine Pitrou <antoine at python.org>
(cherry picked from commit 056c08211b402b4dbc1530a9de9d00ad5309909f)

files:
A Misc/NEWS.d/next/C API/2020-03-24-09-27-10.bpo-40052.27P2KG.rst
M Include/cpython/abstract.h
M Objects/call.c

diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h
index 7bc80833a746e..0f1304d26af33 100644
--- a/Include/cpython/abstract.h
+++ b/Include/cpython/abstract.h
@@ -67,7 +67,7 @@ PyVectorcall_Function(PyObject *callable)
 {
     PyTypeObject *tp;
     Py_ssize_t offset;
-    vectorcallfunc *ptr;
+    vectorcallfunc ptr;
 
     assert(callable != NULL);
     tp = Py_TYPE(callable);
@@ -77,8 +77,8 @@ PyVectorcall_Function(PyObject *callable)
     assert(PyCallable_Check(callable));
     offset = tp->tp_vectorcall_offset;
     assert(offset > 0);
-    ptr = (vectorcallfunc *)(((char *)callable) + offset);
-    return *ptr;
+    memcpy(&ptr, (char *) callable + offset, sizeof(ptr));
+    return ptr;
 }
 
 /* Call the callable object 'callable' with the "vectorcall" calling
diff --git a/Misc/NEWS.d/next/C API/2020-03-24-09-27-10.bpo-40052.27P2KG.rst b/Misc/NEWS.d/next/C API/2020-03-24-09-27-10.bpo-40052.27P2KG.rst
new file mode 100644
index 0000000000000..538488e2fbacc
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2020-03-24-09-27-10.bpo-40052.27P2KG.rst	
@@ -0,0 +1,2 @@
+Fix an alignment build warning/error in function ``PyVectorcall_Function()``.
+Patch by Andreas Schneider, Antoine Pitrou and Petr Viktorin.
diff --git a/Objects/call.c b/Objects/call.c
index 61426c7e09e4e..87dc0dbbdb504 100644
--- a/Objects/call.c
+++ b/Objects/call.c
@@ -205,6 +205,7 @@ PyObject *
 PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
 {
     PyThreadState *tstate = _PyThreadState_GET();
+    vectorcallfunc func;
 
     /* get vectorcallfunc as in PyVectorcall_Function, but without
      * the Py_TPFLAGS_HAVE_VECTORCALL check */
@@ -215,7 +216,7 @@ PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
                       Py_TYPE(callable)->tp_name);
         return NULL;
     }
-    vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
+    memcpy(&func, (char *) callable + offset, sizeof(func));
     if (func == NULL) {
         _PyErr_Format(tstate, PyExc_TypeError,
                       "'%.200s' object does not support vectorcall",



More information about the Python-checkins mailing list