Skip to content

Commit 2c2fd96

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 c7d4e57 commit 2c2fd96

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
@@ -104,22 +104,24 @@ static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
104104

105105
#if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE)
106106

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

113-
if (offset >= sizeof(value)) {
113+
if (offset >= CONFIG_BT_DEVICE_NAME_MAX) {
114114
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
115115
}
116116

117-
if (offset + len >= sizeof(value)) {
117+
if (offset + len > CONFIG_BT_DEVICE_NAME_MAX) {
118118
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
119119
}
120120

121121
memcpy(value, buf, len);
122122

123+
value[len] = '\0';
124+
123125
bt_set_name(value);
124126

125127
return len;

0 commit comments

Comments
 (0)