Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 1 | From aa8ee5e5e934908f0357364f6ec90a3ecda62880 Mon Sep 17 00:00:00 2001 |
| 2 | From: Nicolas Schodet <nico@ni.fr.eu.org> |
| 3 | Date: Mon, 3 Jan 2022 02:37:01 +0100 |
| 4 | Subject: [PATCH] Use Py_ssize_t when parsing buffer length, fix #426 (#427) |
| 5 | |
| 6 | From python 3.9 documentation: |
| 7 | |
| 8 | > For all # variants of formats (s#, y#, etc.), the macro |
| 9 | > PY_SSIZE_T_CLEAN must be defined before including Python.h. On Python |
| 10 | > 3.9 and older, the type of the length argument is Py_ssize_t if the |
| 11 | > PY_SSIZE_T_CLEAN macro is defined, or int otherwise. |
| 12 | |
| 13 | From python 3.8 changes: |
| 14 | |
| 15 | > Use of # variants of formats in parsing or building value (e.g. |
| 16 | > PyArg_ParseTuple(), Py_BuildValue(), PyObject_CallFunction(), etc.) |
| 17 | > without PY_SSIZE_T_CLEAN defined raises DeprecationWarning now. It |
| 18 | > will be removed in 3.10 or 4.0. Read Parsing arguments and building |
| 19 | > values for detail. (Contributed by Inada Naoki in bpo-36381.) |
| 20 | |
| 21 | Fixes https://github.com/pybluez/pybluez/issues/426 |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 22 | |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame^] | 23 | Upstream-Status: Backport [https://github.com/pybluez/pybluez/pull/427] |
| 24 | Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl> |
| 25 | --- |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 26 | bluez/btmodule.c | 23 ++++++++++++++--------- |
| 27 | msbt/_msbt.c | 6 ++++-- |
| 28 | 2 files changed, 18 insertions(+), 11 deletions(-) |
| 29 | |
| 30 | diff --git a/bluez/btmodule.c b/bluez/btmodule.c |
| 31 | index 518b723..912a489 100644 |
| 32 | --- a/bluez/btmodule.c |
| 33 | +++ b/bluez/btmodule.c |
| 34 | @@ -16,7 +16,8 @@ Local naming conventions: |
| 35 | - names starting with bt_ are module-level functions |
| 36 | |
| 37 | */ |
| 38 | - |
| 39 | +#define PY_SSIZE_T_CLEAN 1 |
| 40 | +#include "Python.h" |
| 41 | #include "btmodule.h" |
| 42 | #include "structmember.h" |
| 43 | |
| 44 | @@ -732,7 +733,7 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args) |
| 45 | int optname; |
| 46 | int res; |
| 47 | void *buf; |
| 48 | - int buflen; |
| 49 | + Py_ssize_t buflen; |
| 50 | int flag; |
| 51 | |
| 52 | if (PyArg_ParseTuple(args, "iii:setsockopt", &level, &optname, &flag)) { |
| 53 | @@ -2001,7 +2002,8 @@ static PyObject * |
| 54 | bt_hci_send_cmd(PyObject *self, PyObject *args) |
| 55 | { |
| 56 | PySocketSockObject *socko = NULL; |
| 57 | - int err, plen = 0; |
| 58 | + int err; |
| 59 | + Py_ssize_t plen = 0; |
| 60 | uint16_t ogf, ocf; |
| 61 | char *param = NULL; |
| 62 | int dd = 0; |
| 63 | @@ -2036,6 +2038,7 @@ bt_hci_send_req(PyObject *self, PyObject *args, PyObject *kwds) |
| 64 | int err; |
| 65 | int to=0; |
| 66 | char rparam[256]; |
| 67 | + Py_ssize_t req_clen; |
| 68 | struct hci_request req = { 0 }; |
| 69 | int dd = 0; |
| 70 | |
| 71 | @@ -2043,9 +2046,10 @@ bt_hci_send_req(PyObject *self, PyObject *args, PyObject *kwds) |
| 72 | "timeout", 0 }; |
| 73 | |
| 74 | if( !PyArg_ParseTupleAndKeywords(args, kwds, "OHHii|s#i", keywords, |
| 75 | - &socko, &req.ogf, &req.ocf, &req.event, &req.rlen, |
| 76 | - &req.cparam, &req.clen, &to) ) |
| 77 | + &socko, &req.ogf, &req.ocf, &req.event, &req.rlen, |
| 78 | + &req.cparam, &req_clen, &to) ) |
| 79 | return 0; |
| 80 | + req.clen = req_clen; |
| 81 | |
| 82 | req.rparam = rparam; |
| 83 | dd = socko->sock_fd; |
| 84 | @@ -2274,7 +2278,8 @@ Returns the name of the device, or raises an error on failure"); |
| 85 | static PyObject * bt_hci_filter_ ## name (PyObject *self, PyObject *args )\ |
| 86 | { \ |
| 87 | char *param; \ |
| 88 | - int len, arg; \ |
| 89 | + Py_ssize_t len; \ |
| 90 | + int arg; \ |
| 91 | if( !PyArg_ParseTuple(args,"s#i", ¶m, &len, &arg) ) \ |
| 92 | return 0; \ |
| 93 | if( len != sizeof(struct hci_filter) ) { \ |
| 94 | @@ -2303,7 +2308,7 @@ DECL_HCI_FILTER_OP_1(test_opcode, "test opcode!") |
| 95 | static PyObject * bt_hci_filter_ ## name (PyObject *self, PyObject *args )\ |
| 96 | { \ |
| 97 | char *param; \ |
| 98 | - int len; \ |
| 99 | + Py_ssize_t len; \ |
| 100 | if( !PyArg_ParseTuple(args,"s#", ¶m, &len) ) \ |
| 101 | return 0; \ |
| 102 | if( len != sizeof(struct hci_filter) ) { \ |
| 103 | @@ -2364,7 +2369,7 @@ static PyObject * |
| 104 | bt_ba2str(PyObject *self, PyObject *args) |
| 105 | { |
| 106 | char *data=NULL; |
| 107 | - int len=0; |
| 108 | + Py_ssize_t len=0; |
| 109 | char ba_str[19] = {0}; |
| 110 | if (!PyArg_ParseTuple(args, "s#", &data, &len)) return 0; |
| 111 | ba2str((bdaddr_t*)data, ba_str); |
| 112 | @@ -2579,7 +2584,7 @@ bt_sdp_advertise_service( PyObject *self, PyObject *args ) |
| 113 | *provider = NULL, |
| 114 | *description = NULL; |
| 115 | PyObject *service_classes, *profiles, *protocols; |
| 116 | - int namelen = 0, provlen = 0, desclen = 0; |
| 117 | + Py_ssize_t namelen = 0, provlen = 0, desclen = 0; |
| 118 | uuid_t svc_uuid = { 0 }; |
| 119 | int i; |
| 120 | char addrbuf[256] = { 0 }; |
| 121 | diff --git a/msbt/_msbt.c b/msbt/_msbt.c |
| 122 | index b3d27ff..81f5ee9 100644 |
| 123 | --- a/msbt/_msbt.c |
| 124 | +++ b/msbt/_msbt.c |
| 125 | @@ -2,6 +2,8 @@ |
| 126 | #define UNICODE |
| 127 | #endif |
| 128 | |
| 129 | +#define PY_SSIZE_T_CLEAN 1 |
| 130 | + |
| 131 | #include <winsock2.h> |
| 132 | #include <ws2bth.h> |
| 133 | #include <BluetoothAPIs.h> |
| 134 | @@ -155,7 +157,7 @@ static PyObject * |
| 135 | msbt_bind(PyObject *self, PyObject *args) |
| 136 | { |
| 137 | wchar_t *addrstr = NULL; |
| 138 | - int addrstrlen = -1; |
| 139 | + Py_ssize_t addrstrlen = -1; |
| 140 | int sockfd = -1; |
| 141 | int port = -1; |
| 142 | char buf[100] = { 0 }; |
| 143 | @@ -765,7 +767,7 @@ msbt_set_service_raw(PyObject *self, PyObject *args) |
| 144 | WSAESETSERVICEOP op; |
| 145 | |
| 146 | char *record = NULL; |
| 147 | - int reclen = -1; |
| 148 | + Py_ssize_t reclen = -1; |
| 149 | BTH_SET_SERVICE *si = NULL; |
| 150 | int silen = -1; |
| 151 | ULONG sdpVersion = BTH_SDP_VERSION; |
| 152 | -- |
| 153 | 2.34.1 |
| 154 | |