aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2024-10-14 16:40:34 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2024-10-15 13:38:39 +0200
commitb8af11af6027e83f741c4ee6e19b8e03b57d5dfa (patch)
tree1351d2b80fab48922d2396074d33d1bba1457466
parenta1df680b0f92fe70920cedbada8e9969a582f82b (diff)
Fix signature handling for 32bit
Use PyLong_FromVoidPtr()/PyLong_AsVoidPtr() to pass addresses instead of converting to size_t, which can cause signedness issues when using it with the 'n' format of Py_BuildValue(). Split off a helper function taking an address from address_ptr_to_stringlist(), avoiding a conversion. Fixes: PYSIDE-2891 Change-Id: I375311fa910a66b776e6355b0664bb5364fbdab7 Reviewed-by: Fabian Vogt <fabian@ritter-vogt.de> Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--sources/shiboken6/libshiboken/signature/signature.cpp14
-rw-r--r--sources/shiboken6/libshiboken/signature/signature_helper.cpp30
-rw-r--r--sources/shiboken6/libshiboken/signature_p.h1
3 files changed, 26 insertions, 19 deletions
diff --git a/sources/shiboken6/libshiboken/signature/signature.cpp b/sources/shiboken6/libshiboken/signature/signature.cpp
index e69de193f..f002b886a 100644
--- a/sources/shiboken6/libshiboken/signature/signature.cpp
+++ b/sources/shiboken6/libshiboken/signature/signature.cpp
@@ -343,7 +343,7 @@ static int PySide_BuildSignatureArgs(PyObject *obtype_mod, const char *signature
* address of a string array. It will not be turned into a real
* string list until really used by Python. This is quite optimal.
*/
- AutoDecRef numkey(Py_BuildValue("n", signatures));
+ AutoDecRef numkey(PyLong_FromVoidPtr(signatures));
if (type_key.isNull() || numkey.isNull()
|| PyDict_SetItem(pyside_globals->arg_dict, type_key, numkey) < 0)
return -1;
@@ -354,10 +354,13 @@ static int PySide_BuildSignatureArgs(PyObject *obtype_mod, const char *signature
return PyDict_SetItem(pyside_globals->map_dict, type_key, obtype_mod) == 0 ? 0 : -1;
}
-static int PySide_BuildSignatureArgsByte(PyObject *obtype_mod, const uint8_t signatures[], size_t size)
+static int PySide_BuildSignatureArgsByte(PyObject *obtype_mod, const uint8_t *signatures,
+ size_t size)
{
AutoDecRef type_key(GetTypeKey(obtype_mod));
- AutoDecRef numkey(Py_BuildValue("(nn)", signatures, size));
+ AutoDecRef numkey(PyTuple_New(2));
+ PyTuple_SetItem(numkey.object(), 0, PyLong_FromVoidPtr(const_cast<uint8_t *>(signatures)));
+ PyTuple_SetItem(numkey.object(), 1, PyLong_FromSize_t(size));
if (type_key.isNull() || numkey.isNull()
|| PyDict_SetItem(pyside_globals->arg_dict, type_key, numkey) < 0)
return -1;
@@ -435,13 +438,12 @@ PyObject *PySide_BuildSignatureProps(PyObject *type_key)
if (PyTuple_Check(numkey)) {
PyObject *obAddress = PyTuple_GetItem(numkey, 0);
PyObject *obSize = PyTuple_GetItem(numkey, 1);
- const size_t addr = PyLong_AsSize_t(obAddress);
+ const void *addr = PyLong_AsVoidPtr(obAddress);
const Py_ssize_t size = PyLong_AsSsize_t(obSize);
const char **cstrings = bytesToStrings(reinterpret_cast<const uint8_t *>(addr), size);
if (cstrings == nullptr)
return nullptr;
- AutoDecRef locKey(Py_BuildValue("n", cstrings));
- strings.reset(_address_to_stringlist(locKey));
+ strings.reset(_address_ptr_to_stringlist(cstrings));
} else {
strings.reset(_address_to_stringlist(numkey));
}
diff --git a/sources/shiboken6/libshiboken/signature/signature_helper.cpp b/sources/shiboken6/libshiboken/signature/signature_helper.cpp
index e3a4b168a..04b411825 100644
--- a/sources/shiboken6/libshiboken/signature/signature_helper.cpp
+++ b/sources/shiboken6/libshiboken/signature/signature_helper.cpp
@@ -271,24 +271,13 @@ PyObject *_get_class_of_descr(PyObject *ob)
return PyObject_GetAttr(ob, PyMagicName::objclass());
}
-PyObject *_address_to_stringlist(PyObject *numkey)
+PyObject *_address_ptr_to_stringlist(const char **sig_strings)
{
- /*
- * This is a tiny optimization that saves initialization time.
- * Instead of creating all Python strings during the call to
- * `PySide_BuildSignatureArgs`, we store the address of the stringlist.
- * When needed in `PySide_BuildSignatureProps`, the strings are
- * finally materialized.
- */
- Py_ssize_t address = PyNumber_AsSsize_t(numkey, PyExc_ValueError);
- if (address == -1 && PyErr_Occurred())
- return nullptr;
- char **sig_strings = reinterpret_cast<char **>(address);
PyObject *res_list = PyList_New(0);
if (res_list == nullptr)
return nullptr;
for (; *sig_strings != nullptr; ++sig_strings) {
- char *sig_str = *sig_strings;
+ const char *sig_str = *sig_strings;
AutoDecRef pystr(Py_BuildValue("s", sig_str));
if (pystr.isNull() || PyList_Append(res_list, pystr) < 0)
return nullptr;
@@ -296,6 +285,21 @@ PyObject *_address_to_stringlist(PyObject *numkey)
return res_list;
}
+PyObject *_address_to_stringlist(PyObject *numkey)
+{
+ /*
+ * This is a tiny optimization that saves initialization time.
+ * Instead of creating all Python strings during the call to
+ * `PySide_BuildSignatureArgs`, we store the address of the stringlist.
+ * When needed in `PySide_BuildSignatureProps`, the strings are
+ * finally materialized.
+ */
+ void *address = PyLong_AsVoidPtr(numkey);
+ if (address == nullptr && PyErr_Occurred())
+ return nullptr;
+ return _address_ptr_to_stringlist(reinterpret_cast<const char **>(address));
+}
+
int _build_func_to_type(PyObject *obtype)
{
/*
diff --git a/sources/shiboken6/libshiboken/signature_p.h b/sources/shiboken6/libshiboken/signature_p.h
index d0c4ee537..3433034be 100644
--- a/sources/shiboken6/libshiboken/signature_p.h
+++ b/sources/shiboken6/libshiboken/signature_p.h
@@ -63,6 +63,7 @@ PyObject *_get_class_of_cf(PyObject *ob_cf);
PyObject *_get_class_of_sm(PyObject *ob_sm);
PyObject *_get_class_of_descr(PyObject *ob);
PyObject *_address_to_stringlist(PyObject *numkey);
+PyObject *_address_ptr_to_stringlist(const char **sig_strings);
int _build_func_to_type(PyObject *obtype);
int _finish_nested_classes(PyObject *dict);