Skip to contentSkip to conversation

client/gatt: Fix using atoi

PATCH Superseded Luiz Augusto von Dentz 3 participants 4 messages Nov 8, 2021 – Nov 9, 2021 Bluetooth drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/1 client/gatt: Fix using atoi

From: Luiz Augusto von Dentz <[email protected]>

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 strtol.

---
 client/gatt.c | 71 +++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 11 deletions(-)
client/gatt.c +60 -11
index 21fd38ecf..12e213d0f 100644
@@ -650,19 +650,27 @@ static void read_attribute(GDBusProxy *proxy, uint16_t offset)
 void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[])
 {
 	const char *iface;
-	uint16_t offset = 0;
+	long 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) {
+			char *endptr = NULL;
+
+			offset = strtol(argv[1], &endptr, 0);
+			if (!endptr || *endptr != '\0' || offset > UINT16_MAX) {
+				bt_shell_printf("Invalid offload: %s", argv[1]);
+				goto done;
+			}
+		}
 
 		read_attribute(proxy, offset);
 		return;
 	}
 
+done:
 	bt_shell_printf("Unable to read attribute %s\n",
 						g_dbus_proxy_get_path(proxy));
 	return bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -805,8 +813,18 @@ 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) {
+			char *endptr = NULL;
+			long offset;
+
+			offset = strtol(argv[1], &endptr, 0);
+			if (!endptr || *endptr != '\0' || offset > UINT16_MAX) {
+				bt_shell_printf("Invalid offload: %s", argv[1]);
+				goto fail;
+			}
+
+			data.offset = offset;
+		}
 
 		if (argc > 3)
 			data.type = argv[3];
@@ -815,6 +833,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));
 
@@ -1482,8 +1501,18 @@ 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) {
+		char *endptr = NULL;
+		long handle;
+
+		handle = strtol(argv[2], &endptr, 0);
+		if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+			bt_shell_printf("Invalid handle: %s", argv[2]);
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+
+		service->handle = handle;
+	}
 
 	if (g_dbus_register_interface(conn, service->path,
 					SERVICE_INTERFACE, NULL, NULL,
@@ -2574,8 +2603,18 @@ 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) {
+		char *endptr = NULL;
+		long handle;
+
+		handle = strtol(argv[3], &endptr, 0);
+		if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+			bt_shell_printf("Invalid handle: %s", argv[3]);
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+
+		chrc->handle = handle;
+	}
 
 	if (g_dbus_register_interface(conn, chrc->path, CHRC_INTERFACE,
 					chrc_methods, NULL, chrc_properties,
@@ -2851,8 +2890,18 @@ 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) {
+		char *endptr = NULL;
+		long handle;
+
+		handle = strtol(argv[3], &endptr, 0);
+		if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+			bt_shell_printf("Invalid handle: %s", argv[3]);
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+
+		desc->handle = handle;
+	}
 
 	if (g_dbus_register_interface(conn, desc->path, DESC_INTERFACE,
 					desc_methods, NULL, desc_properties,
Signature · 1 line
2.31.1
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=577055

---Test result---

Test Summary:

CheckPatch                    FAIL      1.51 seconds
GitLint                       PASS      0.93 seconds
Prep - Setup ELL              PASS      51.58 seconds
Build - Prep                  PASS      0.47 seconds
Build - Configure             PASS      9.50 seconds
Build - Make                  PASS      215.23 seconds
Make Check                    PASS      9.67 seconds
Make Distcheck                PASS      256.66 seconds
Build w/ext ELL - Configure   PASS      9.57 seconds
Build w/ext ELL - Make        PASS      204.08 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script with rule in .checkpatch.conf Output:
[BlueZ] client/gatt: Fix using atoi
WARNING:TYPO_SPELLING: 'prefered' may be misspelled - perhaps 'preferred'? #84:
likely the prefered format for the likes of handles, etc, so this

           ^^^^^^^^

/github/workspace/src/12608969.patch total: 0 errors, 1 warnings, 117 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/12608969.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 26 lines

-----Original Message-----
From: Luiz Augusto von Dentz <[email protected]>
Sent: maandag 8 november 2021 20:18
To: [email protected]
Subject: [PATCH BlueZ] client/gatt: Fix using atoi

From: Luiz Augusto von Dentz <[email protected]>

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 strtol.

---
 client/gatt.c | 71 +++++++++++++++++++++++++++++++++++++++++++----
----
 1 file changed, 60 insertions(+), 11 deletions(-)
client/gatt.c +0 -0
@@ -650,19 +650,27 @@ static void read_attribute(GDBusProxy *proxy,

uint16_t offset) void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]) {

client/gatt.c +1 -1
 	const char *iface;
-	uint16_t offset = 0;
+	long offset = 0;
 

client/gatt.c static void read_attribute(GDBusProxy *proxy, uint16_t offset) · line 662

Quoted 10 lines
client/gatt.c +4 -2
 	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) {
+			char *endptr = NULL;
+
+			offset = strtol(argv[1], &endptr, 0);

Perhaps it is better to use strtoul and make offset an unsigned long or check for < 0 when C99 is not used as read_attribute takes it as a uint16_t? And from the naming of the other variables that are assigned using strtol I would guess more or less the same applies to those strtol calls.

Quoted 115 lines
client/gatt.c +1 -0
+			if (!endptr || *endptr != '\0' || offset >

UINT16_MAX) {

client/gatt.c +5 -0
+				bt_shell_printf("Invalid offload: %s", argv[1]);
+				goto done;
+			}
+		}
 
 		read_attribute(proxy, offset);
 		return;
 	}
 
+done:
 	bt_shell_printf("Unable to read attribute %s\n",
	g_dbus_proxy_get_path(proxy));
client/gatt.c +12 -2
 	return bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -805,8 +813,18 @@ 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) {
+			char *endptr = NULL;
+			long offset;
+
+			offset = strtol(argv[1], &endptr, 0);
+			if (!endptr || *endptr != '\0' || offset >
 UINT16_MAX) {
+				bt_shell_printf("Invalid offload: %s", argv[1]);
+				goto fail;
+			}
+
+			data.offset = offset;
+		}
 
client/gatt.c +1 -0
 		if (argc > 3)
 			data.type = argv[3];
@@ -815,6 +833,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));
client/gatt.c +12 -2
@@ -1482,8 +1501,18 @@ 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) {
+		char *endptr = NULL;
+		long handle;
+
+		handle = strtol(argv[2], &endptr, 0);
+		if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+			bt_shell_printf("Invalid handle: %s", argv[2]);
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+
+		service->handle = handle;
+	}
 
 	if (g_dbus_register_interface(conn, service->path,
client/gatt.c +12 -2
 					SERVICE_INTERFACE, NULL, NULL,
@@ -2574,8 +2603,18 @@ 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) {
+		char *endptr = NULL;
+		long handle;
+
+		handle = strtol(argv[3], &endptr, 0);
+		if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+			bt_shell_printf("Invalid handle: %s", argv[3]);
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+
+		chrc->handle = handle;
+	}
 
client/gatt.c +12 -2
 	if (g_dbus_register_interface(conn, chrc->path, CHRC_INTERFACE,
 					chrc_methods, NULL, chrc_properties,
@@ -2851,8 +2890,18 @@ 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) {
+		char *endptr = NULL;
+		long handle;
+
+		handle = strtol(argv[3], &endptr, 0);
+		if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+			bt_shell_printf("Invalid handle: %s", argv[3]);
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+
+		desc->handle = handle;
+	}
 
 	if (g_dbus_register_interface(conn, desc->path, DESC_INTERFACE,
Quoted 4 lines

desc_methods, NULL,
desc_properties,
--
2.31.1

Kind regards,
Thiemo

author maintainer
#3 lore raw

Hi Thiemo,

Quoted 33 lines · Thiemo van Engelen

Hi Luiz,

Quoted 30 lines

-----Original Message-----
From: Luiz Augusto von Dentz <[email protected]>
Sent: maandag 8 november 2021 20:18
To: [email protected]
Subject: [PATCH BlueZ] client/gatt: Fix using atoi

From: Luiz Augusto von Dentz <[email protected]>

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 strtol.

---
 client/gatt.c | 71 +++++++++++++++++++++++++++++++++++++++++++----
----
 1 file changed, 60 insertions(+), 11 deletions(-)
client/gatt.c +0 -0
@@ -650,19 +650,27 @@ static void read_attribute(GDBusProxy *proxy,

uint16_t offset) void gatt_read_attribute(GDBusProxy *proxy, int argc, char

*argv[])  {
      const char *iface;
client/gatt.c +1 -1
-     uint16_t offset = 0;
+     long offset = 0;
 
      iface = g_dbus_proxy_get_interface(proxy);
      if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
                              !strcmp(iface, "org.bluez.GattDescriptor1")) {
Quoted 9 lines

client/gatt.c static void read_attribute(GDBusProxy *proxy, uint16_t offset) · line 662

Quoted 6 lines
client/gatt.c +4 -2
-             if (argc == 2)
-                     offset = atoi(argv[1]);
+             if (argc == 2) {
+                     char *endptr = NULL;
+
+                     offset = strtol(argv[1], &endptr, 0);

Perhaps it is better to use strtoul and make offset an unsigned long or check for < 0 when C99 is not used as read_attribute takes it as a uint16_t? And from the naming of the other variables that are assigned using strtol I would guess more or less the same applies to those strtol calls.

Yep, I forgot about strtoul existed but you are right that for these values we can definitely use it.

Quoted 122 lines
Quoted 115 lines

+ if (!endptr || *endptr != '\0' || offset >
UINT16_MAX) {

client/gatt.c +17 -2
+                             bt_shell_printf("Invalid offload: %s", argv[1]);
+                             goto done;
+                     }
+             }
 
              read_attribute(proxy, offset);
              return;
      }
 
+done:
      bt_shell_printf("Unable to read attribute %s\n",
 
      g_dbus_proxy_get_path(proxy));
      return bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -805,8 +813,18 @@ 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) {
+                     char *endptr = NULL;
+                     long offset;
+
+                     offset = strtol(argv[1], &endptr, 0);
+                     if (!endptr || *endptr != '\0' || offset >
 UINT16_MAX) {
+                             bt_shell_printf("Invalid offload: %s", argv[1]);
+                             goto fail;
+                     }
+
+                     data.offset = offset;
+             }
 
              if (argc > 3)
                      data.type = argv[3];
client/gatt.c +1 -0
@@ -815,6 +833,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));
client/gatt.c +12 -2
@@ -1482,8 +1501,18 @@ 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) {
+             char *endptr = NULL;
+             long handle;
+
+             handle = strtol(argv[2], &endptr, 0);
+             if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+                     bt_shell_printf("Invalid handle: %s", argv[2]);
+                     return bt_shell_noninteractive_quit(EXIT_FAILURE);
+             }
+
+             service->handle = handle;
+     }
 
      if (g_dbus_register_interface(conn, service->path,

SERVICE_INTERFACE, NULL, NULL,

client/gatt.c +12 -2
@@ -2574,8 +2603,18 @@ 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) {
+             char *endptr = NULL;
+             long handle;
+
+             handle = strtol(argv[3], &endptr, 0);
+             if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+                     bt_shell_printf("Invalid handle: %s", argv[3]);
+                     return bt_shell_noninteractive_quit(EXIT_FAILURE);
+             }
+
+             chrc->handle = handle;
+     }
 

if (g_dbus_register_interface(conn, chrc->path, CHRC_INTERFACE,
chrc_methods, NULL, chrc_properties,

client/gatt.c +12 -2
@@ -2851,8 +2890,18 @@ 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) {
+             char *endptr = NULL;
+             long handle;
+
+             handle = strtol(argv[3], &endptr, 0);
+             if (!endptr || *endptr != '\0' || handle > UINT16_MAX) {
+                     bt_shell_printf("Invalid handle: %s", argv[3]);
+                     return bt_shell_noninteractive_quit(EXIT_FAILURE);
+             }
+
+             desc->handle = handle;
+     }
 
      if (g_dbus_register_interface(conn, desc->path, DESC_INTERFACE,
Quoted 4 lines

desc_methods, NULL,
desc_properties,
--
2.31.1

Kind regards,
Thiemo

Signature · 1 line
Luiz Augusto von Dentz