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