Skip to content

Commit 5b834c9

Browse files
theob-prom-alperen-sener
authored andcommitted
[nrf fromtree] Bluetooth: Host: GAP Device Name write now add null char at the end
The function used to write the value of the GAP Device Name characteristic now ensure that the string passed to `bt_set_name` is null terminated. Also fix a wrong offset calculation. The function used to write the value of the GAP Device Name characteristic was returning an error when the offset + the length of data to write was superior **or equal** to the maximum size of the device name. This caused the actual maximum device name size to be reduced by 1 byte. Signed-off-by: Théo Battrel <[email protected]> (cherry picked from commit 972d4c7)
1 parent ab7749a commit 5b834c9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

subsys/bluetooth/host/gatt.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,24 @@ static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
101101

102102
#if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE)
103103

104-
static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
105-
const void *buf, uint16_t len, uint16_t offset,
106-
uint8_t flags)
104+
static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf,
105+
uint16_t len, uint16_t offset, uint8_t flags)
107106
{
108-
char value[CONFIG_BT_DEVICE_NAME_MAX] = {};
107+
/* adding one to fit the terminating null character */
108+
char value[CONFIG_BT_DEVICE_NAME_MAX + 1] = {};
109109

110-
if (offset >= sizeof(value)) {
110+
if (offset >= CONFIG_BT_DEVICE_NAME_MAX) {
111111
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
112112
}
113113

114-
if (offset + len >= sizeof(value)) {
114+
if (offset + len > CONFIG_BT_DEVICE_NAME_MAX) {
115115
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
116116
}
117117

118118
memcpy(value, buf, len);
119119

120+
value[len] = '\0';
121+
120122
bt_set_name(value);
121123

122124
return len;

0 commit comments

Comments
 (0)