From d76bba4b5ef85c6b25dfdf9c9809fcead0f4da5c Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Fri, 20 Aug 2021 09:40:42 +0200 Subject: [PATCH] Bluetooth: host: Device name handling of invalid length Add a build assert if the device name has been misconfigured. The device name has a max length of 248. When configured as dynamic make sure that the initial device length can fit in the dynamic max length. This prevents us from having to handle length overflow when setting device name in advertising data which has an 8-bit length field. Log a warning if failing to set the device name in bt_enable. Remove unused defines in the shell. Signed-off-by: Joakim Andersson --- subsys/bluetooth/host/hci_core.c | 12 +++++++++++- subsys/bluetooth/shell/bt.c | 3 --- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index ce1f6ec3d02..8099dc3620e 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -3541,7 +3541,10 @@ int bt_enable(bt_ready_cb_t cb) return err; } } else { - bt_set_name(CONFIG_BT_DEVICE_NAME); + err = bt_set_name(CONFIG_BT_DEVICE_NAME); + if (err) { + BT_WARN("Failed to set device name (%d)", err); + } } ready_cb = cb; @@ -3584,6 +3587,13 @@ int bt_enable(bt_ready_cb_t cb) return 0; } +#define DEVICE_NAME_LEN (sizeof(CONFIG_BT_DEVICE_NAME) - 1) +#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC) +BUILD_ASSERT(DEVICE_NAME_LEN < CONFIG_BT_DEVICE_NAME_MAX); +#else +BUILD_ASSERT(DEVICE_NAME_LEN < 248); +#endif + int bt_set_name(const char *name) { #if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC) diff --git a/subsys/bluetooth/shell/bt.c b/subsys/bluetooth/shell/bt.c index 767798f4d2c..64689f12887 100644 --- a/subsys/bluetooth/shell/bt.c +++ b/subsys/bluetooth/shell/bt.c @@ -36,9 +36,6 @@ #include "ll.h" #include "hci.h" -#define DEVICE_NAME CONFIG_BT_DEVICE_NAME -#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) - /* Multiply bt 1.25 to get MS */ #define BT_INTERVAL_TO_MS(interval) ((interval) * 5 / 4)