From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2 BlueZ] client/gatt: Fix using atoi
Date: Tue, 9 Nov 2021 13:37:21 -0800 [thread overview]
Message-ID: <20211109213721.1121677-1-luiz.dentz@gmail.com> (raw)
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
atoi doesn't support values entered in hexadecimal (0x...) which is
likely the prefered format for the likes of handles, etc, so this
replaces the uses of atoi with strtoul.
---
client/gatt.c | 71 +++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 60 insertions(+), 11 deletions(-)
diff --git a/client/gatt.c b/client/gatt.c
index 21fd38ecf..ce13b3341 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -647,22 +647,40 @@ static void read_attribute(GDBusProxy *proxy, uint16_t offset)
bt_shell_printf("Attempting to read %s\n", g_dbus_proxy_get_path(proxy));
}
+static int parse_offset(const char *arg)
+{
+ char *endptr = NULL;
+ unsigned long offset;
+
+ offset = strtoul(arg, &endptr, 0);
+ if (!endptr || *endptr != '\0' || offset > UINT16_MAX) {
+ bt_shell_printf("Invalid offload: %s", arg);
+ return -EINVAL;
+ }
+
+ return offset;
+}
+
void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[])
{
const char *iface;
- uint16_t offset = 0;
+ int offset = 0;
iface = g_dbus_proxy_get_interface(proxy);
if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
!strcmp(iface, "org.bluez.GattDescriptor1")) {
- if (argc == 2)
- offset = atoi(argv[1]);
+ if (argc == 2) {
+ offset = parse_offset(argv[1]);
+ if (offset < 0)
+ goto fail;
+ }
read_attribute(proxy, offset);
return;
}
+fail:
bt_shell_printf("Unable to read attribute %s\n",
g_dbus_proxy_get_path(proxy));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -805,8 +823,15 @@ void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[])
!strcmp(iface, "org.bluez.GattDescriptor1")) {
data.iov.iov_base = str2bytearray(argv[1], &data.iov.iov_len);
- if (argc > 2)
- data.offset = atoi(argv[2]);
+ if (argc > 2) {
+ int offset;
+
+ offset = parse_offset(argv[2]);
+ if (offset < 0)
+ goto fail;
+
+ data.offset = offset;
+ }
if (argc > 3)
data.type = argv[3];
@@ -815,6 +840,7 @@ void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[])
return;
}
+fail:
bt_shell_printf("Unable to write attribute %s\n",
g_dbus_proxy_get_path(proxy));
@@ -1469,6 +1495,20 @@ static void service_set_primary(const char *input, void *user_data)
}
}
+static uint16_t parse_handle(const char *arg)
+{
+ char *endptr = NULL;
+ unsigned long handle;
+
+ handle = strtoul(arg, &endptr, 0);
+ if (!endptr || *endptr != '\0' || !handle || handle > UINT16_MAX) {
+ bt_shell_printf("Invalid handle: %s", arg);
+ return 0;
+ }
+
+ return handle;
+}
+
void gatt_register_service(DBusConnection *conn, GDBusProxy *proxy,
int argc, char *argv[])
{
@@ -1482,8 +1522,11 @@ void gatt_register_service(DBusConnection *conn, GDBusProxy *proxy,
g_list_length(local_services));
service->primary = primary;
- if (argc > 2)
- service->handle = atoi(argv[2]);
+ if (argc > 2) {
+ service->handle = parse_handle(argv[2]);
+ if (!service->handle)
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+ }
if (g_dbus_register_interface(conn, service->path,
SERVICE_INTERFACE, NULL, NULL,
@@ -2574,8 +2617,11 @@ void gatt_register_chrc(DBusConnection *conn, GDBusProxy *proxy,
chrc->flags = g_strsplit(argv[2], ",", -1);
chrc->authorization_req = attr_authorization_flag_exists(chrc->flags);
- if (argc > 3)
- chrc->handle = atoi(argv[3]);
+ if (argc > 3) {
+ chrc->handle = parse_handle(argv[3]);
+ if (!chrc->handle)
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+ }
if (g_dbus_register_interface(conn, chrc->path, CHRC_INTERFACE,
chrc_methods, NULL, chrc_properties,
@@ -2851,8 +2897,11 @@ void gatt_register_desc(DBusConnection *conn, GDBusProxy *proxy,
g_list_length(desc->chrc->descs));
desc->flags = g_strsplit(argv[2], ",", -1);
- if (argc > 3)
- desc->handle = atoi(argv[3]);
+ if (argc > 3) {
+ desc->handle = parse_handle(argv[3]);
+ if (!desc->handle)
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+ }
if (g_dbus_register_interface(conn, desc->path, DESC_INTERFACE,
desc_methods, NULL, desc_properties,
--
2.31.1
next reply other threads:[~2021-11-09 21:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-09 21:37 Luiz Augusto von Dentz [this message]
2021-11-09 21:54 ` [v2,BlueZ] client/gatt: Fix using atoi bluez.test.bot
2021-11-09 22:46 ` Luiz Augusto von Dentz
-- strict thread matches above, loose matches on Subject: below --
2021-11-09 21:44 [PATCH v2 BlueZ] " Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211109213721.1121677-1-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox