]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40052: Fix alignment issue in PyVectorcall_Function() (GH-23999)
authorPetr Viktorin <encukou@gmail.com>
Tue, 29 Dec 2020 23:32:07 +0000 (00:32 +0100)
committerGitHub <noreply@github.com>
Tue, 29 Dec 2020 23:32:07 +0000 (15:32 -0800)
```
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: Andreas Schneider <asn@cryptomilk.org>
Co-Authored-By: Antoine Pitrou <antoine@python.org>
Include/cpython/abstract.h
Misc/NEWS.d/next/C API/2020-03-24-09-27-10.bpo-40052.27P2KG.rst [new file with mode: 0644]
Objects/call.c

index b5b6e4819788c5dd26e03443f2227a0367c67cab..1083942c14929c5e7f136b877bd24c89c473c8d1 100644 (file)
@@ -63,7 +63,7 @@ PyVectorcall_Function(PyObject *callable)
 {
     PyTypeObject *tp;
     Py_ssize_t offset;
-    vectorcallfunc *ptr;
+    vectorcallfunc ptr;
 
     assert(callable != NULL);
     tp = Py_TYPE(callable);
@@ -73,8 +73,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 (file)
index 0000000..538488e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an alignment build warning/error in function ``PyVectorcall_Function()``.
+Patch by Andreas Schneider, Antoine Pitrou and Petr Viktorin.
index 30fa14ccfd765dd7a5a1aecc2e69941c96b23c01..35b06a9b9c4d739cb6e5c08861b366079ee0bc23 100644 (file)
@@ -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",