From aa8ee5e5e934908f0357364f6ec90a3ecda62880 Mon Sep 17 00:00:00 2001
From: Nicolas Schodet <nico@ni.fr.eu.org>
Date: Mon, 3 Jan 2022 02:37:01 +0100
Subject: [PATCH] Use Py_ssize_t when parsing buffer length, fix #426 (#427)

From python 3.9 documentation:

> For all # variants of formats (s#, y#, etc.), the macro
> PY_SSIZE_T_CLEAN must be defined before including Python.h. On Python
> 3.9 and older, the type of the length argument is Py_ssize_t if the
> PY_SSIZE_T_CLEAN macro is defined, or int otherwise.

From python 3.8 changes:

> Use of # variants of formats in parsing or building value (e.g.
> PyArg_ParseTuple(), Py_BuildValue(), PyObject_CallFunction(), etc.)
> without PY_SSIZE_T_CLEAN defined raises DeprecationWarning now. It
> will be removed in 3.10 or 4.0. Read Parsing arguments and building
> values for detail. (Contributed by Inada Naoki in bpo-36381.)

Fixes https://github.com/pybluez/pybluez/issues/426

Upstream-Status: Backport [https://github.com/pybluez/pybluez/pull/427]
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 bluez/btmodule.c | 23 ++++++++++++++---------
 msbt/_msbt.c     |  6 ++++--
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/bluez/btmodule.c b/bluez/btmodule.c
index 518b723..912a489 100644
--- a/bluez/btmodule.c
+++ b/bluez/btmodule.c
@@ -16,7 +16,8 @@ Local naming conventions:
 - names starting with bt_ are module-level functions
 
 */
-
+#define PY_SSIZE_T_CLEAN 1
+#include "Python.h"
 #include "btmodule.h"
 #include "structmember.h"
 
@@ -732,7 +733,7 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args)
     int optname;
     int res;
     void *buf;
-    int buflen;
+    Py_ssize_t buflen;
     int flag;
 
     if (PyArg_ParseTuple(args, "iii:setsockopt", &level, &optname, &flag)) {
@@ -2001,7 +2002,8 @@ static PyObject *
 bt_hci_send_cmd(PyObject *self, PyObject *args)
 {
     PySocketSockObject *socko = NULL;
-    int err, plen = 0;
+    int err;
+    Py_ssize_t plen = 0;
     uint16_t ogf, ocf;
     char *param = NULL;
     int dd = 0;
@@ -2036,6 +2038,7 @@ bt_hci_send_req(PyObject *self, PyObject *args, PyObject *kwds)
     int err;
     int to=0;
     char rparam[256];
+    Py_ssize_t req_clen;
     struct hci_request req = { 0 };
     int dd = 0;
 
@@ -2043,9 +2046,10 @@ bt_hci_send_req(PyObject *self, PyObject *args, PyObject *kwds)
                                 "timeout", 0 };
 
     if( !PyArg_ParseTupleAndKeywords(args, kwds, "OHHii|s#i", keywords,
-                &socko, &req.ogf, &req.ocf, &req.event, &req.rlen, 
-                &req.cparam, &req.clen, &to) )
+                &socko, &req.ogf, &req.ocf, &req.event, &req.rlen,
+                &req.cparam, &req_clen, &to) )
         return 0;
+    req.clen = req_clen;
 
     req.rparam = rparam;
     dd = socko->sock_fd;
@@ -2274,7 +2278,8 @@ Returns the name of the device, or raises an error on failure");
 static PyObject * bt_hci_filter_ ## name (PyObject *self, PyObject *args )\
 { \
     char *param; \
-    int len, arg; \
+    Py_ssize_t len; \
+    int arg; \
     if( !PyArg_ParseTuple(args,"s#i", &param, &len, &arg) ) \
         return 0; \
     if( len != sizeof(struct hci_filter) ) { \
@@ -2303,7 +2308,7 @@ DECL_HCI_FILTER_OP_1(test_opcode, "test opcode!")
 static PyObject * bt_hci_filter_ ## name (PyObject *self, PyObject *args )\
 { \
     char *param; \
-    int len; \
+    Py_ssize_t len; \
     if( !PyArg_ParseTuple(args,"s#", &param, &len) ) \
         return 0; \
     if( len != sizeof(struct hci_filter) ) { \
@@ -2364,7 +2369,7 @@ static PyObject *
 bt_ba2str(PyObject *self, PyObject *args)
 {
     char *data=NULL;
-    int len=0;
+    Py_ssize_t len=0;
     char ba_str[19] = {0};
     if (!PyArg_ParseTuple(args, "s#", &data, &len)) return 0;
     ba2str((bdaddr_t*)data, ba_str);
@@ -2579,7 +2584,7 @@ bt_sdp_advertise_service( PyObject *self, PyObject *args )
          *provider = NULL, 
          *description = NULL;
     PyObject *service_classes, *profiles, *protocols;
-    int namelen = 0, provlen = 0, desclen = 0;
+    Py_ssize_t namelen = 0, provlen = 0, desclen = 0;
     uuid_t svc_uuid = { 0 };
     int i;
     char addrbuf[256] = { 0 };
diff --git a/msbt/_msbt.c b/msbt/_msbt.c
index b3d27ff..81f5ee9 100644
--- a/msbt/_msbt.c
+++ b/msbt/_msbt.c
@@ -2,6 +2,8 @@
 #define UNICODE
 #endif
 
+#define PY_SSIZE_T_CLEAN 1
+
 #include <winsock2.h>
 #include <ws2bth.h>
 #include <BluetoothAPIs.h>
@@ -155,7 +157,7 @@ static PyObject *
 msbt_bind(PyObject *self, PyObject *args)
 {
     wchar_t *addrstr = NULL;
-    int addrstrlen = -1;
+    Py_ssize_t addrstrlen = -1;
     int sockfd = -1;
     int port = -1;
     char buf[100] = { 0 };
@@ -765,7 +767,7 @@ msbt_set_service_raw(PyObject *self, PyObject *args)
     WSAESETSERVICEOP op;
 
     char *record = NULL;
-    int reclen = -1;
+    Py_ssize_t reclen = -1;
     BTH_SET_SERVICE *si = NULL;
     int silen = -1;
     ULONG sdpVersion = BTH_SDP_VERSION;
-- 
2.34.1

