Skip to content

Commit 1c08475

Browse files
author
Jiang Jiang Jian
committed
Merge branch 'bugfix/classic_bt-safe-string-handling' into 'master'
Bugfix/classic bt safe string handling Closes IDFGH-16085 See merge request espressif/esp-idf!40868
2 parents 2bbf9b6 + 720108b commit 1c08475

File tree

2 files changed

+28
-26
lines changed
  • examples/bluetooth/bluedroid
    • bluedroid_host_only/bluedroid_host_only_uart/main
    • classic_bt/bt_discovery/main

2 files changed

+28
-26
lines changed

examples/bluetooth/bluedroid/bluedroid_host_only/bluedroid_host_only_uart/main/main.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -54,32 +54,32 @@ static app_gap_cb_t m_dev_info;
5454
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
5555
{
5656
if (bda == NULL || str == NULL || size < 18) {
57-
return NULL;
57+
return "";
5858
}
5959

6060
uint8_t *p = bda;
61-
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
61+
snprintf(str, size, "%02x:%02x:%02x:%02x:%02x:%02x",
6262
p[0], p[1], p[2], p[3], p[4], p[5]);
6363
return str;
6464
}
6565

6666
static char *uuid2str(esp_bt_uuid_t *uuid, char *str, size_t size)
6767
{
6868
if (uuid == NULL || str == NULL) {
69-
return NULL;
69+
return "";
7070
}
7171

7272
if (uuid->len == 2 && size >= 5) {
73-
sprintf(str, "%04x", uuid->uuid.uuid16);
73+
snprintf(str, size, "%04x", uuid->uuid.uuid16);
7474
} else if (uuid->len == 4 && size >= 9) {
75-
sprintf(str, "%08"PRIx32, uuid->uuid.uuid32);
75+
snprintf(str, size, "%08"PRIx32, uuid->uuid.uuid32);
7676
} else if (uuid->len == 16 && size >= 37) {
7777
uint8_t *p = uuid->uuid.uuid128;
78-
sprintf(str, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
78+
snprintf(str,size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
7979
p[15], p[14], p[13], p[12], p[11], p[10], p[9], p[8],
8080
p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
8181
} else {
82-
return NULL;
82+
return "";
8383
}
8484

8585
return str;
@@ -128,7 +128,7 @@ static void update_device_info(esp_bt_gap_cb_param_t *param)
128128
uint8_t eir_len = 0;
129129
esp_bt_gap_dev_prop_t *p;
130130

131-
ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, 18));
131+
ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, sizeof(bda_str)));
132132
for (int i = 0; i < param->disc_res.num_prop; i++) {
133133
p = param->disc_res.prop + i;
134134
switch (p->type) {
@@ -231,18 +231,19 @@ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
231231
p_dev->state == APP_GAP_STATE_SERVICE_DISCOVERING) {
232232
p_dev->state = APP_GAP_STATE_SERVICE_DISCOVER_COMPLETE;
233233
if (param->rmt_srvcs.stat == ESP_BT_STATUS_SUCCESS) {
234-
ESP_LOGI(GAP_TAG, "Services for device %s found", bda2str(p_dev->bda, bda_str, 18));
234+
ESP_LOGI(GAP_TAG, "Services for device %s found", bda2str(p_dev->bda, bda_str, sizeof(bda_str)));
235235
for (int i = 0; i < param->rmt_srvcs.num_uuids; i++) {
236236
esp_bt_uuid_t *u = param->rmt_srvcs.uuid_list + i;
237237
ESP_LOGI(GAP_TAG, "--%s", uuid2str(u, uuid_str, 37));
238238
}
239239
} else {
240-
ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, 18));
240+
ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, sizeof(bda_str)));
241241
}
242242
}
243243
break;
244244
}
245245
case ESP_BT_GAP_RMT_SRVC_REC_EVT:
246+
break;
246247
default: {
247248
ESP_LOGI(GAP_TAG, "event: %d", event);
248249
break;
@@ -256,7 +257,7 @@ static void bt_app_gap_start_up(void)
256257
/* register GAP callback function */
257258
esp_bt_gap_register_callback(bt_app_gap_cb);
258259

259-
char *dev_name = "ESP_GAP_INQRUIY";
260+
char *dev_name = "ESP_GAP_INQUIRY";
260261
esp_bt_gap_set_device_name(dev_name);
261262

262263
/* set discoverable and connectable mode, wait to be connected */

examples/bluetooth/bluedroid/classic_bt/bt_discovery/main/main.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,32 @@ static app_gap_cb_t m_dev_info;
5353
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
5454
{
5555
if (bda == NULL || str == NULL || size < 18) {
56-
return NULL;
56+
return "";
5757
}
5858

5959
uint8_t *p = bda;
60-
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
61-
p[0], p[1], p[2], p[3], p[4], p[5]);
60+
snprintf(str, size, "%02x:%02x:%02x:%02x:%02x:%02x",
61+
p[0], p[1], p[2], p[3], p[4], p[5]);
6262
return str;
6363
}
6464

6565
static char *uuid2str(esp_bt_uuid_t *uuid, char *str, size_t size)
6666
{
6767
if (uuid == NULL || str == NULL) {
68-
return NULL;
68+
return "";
6969
}
7070

7171
if (uuid->len == 2 && size >= 5) {
72-
sprintf(str, "%04x", uuid->uuid.uuid16);
72+
snprintf(str, size, "%04x", uuid->uuid.uuid16);
7373
} else if (uuid->len == 4 && size >= 9) {
74-
sprintf(str, "%08"PRIx32, uuid->uuid.uuid32);
74+
snprintf(str, size, "%08"PRIx32, uuid->uuid.uuid32);
7575
} else if (uuid->len == 16 && size >= 37) {
7676
uint8_t *p = uuid->uuid.uuid128;
77-
sprintf(str, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
78-
p[15], p[14], p[13], p[12], p[11], p[10], p[9], p[8],
79-
p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
77+
snprintf(str, size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
78+
p[15], p[14], p[13], p[12], p[11], p[10], p[9], p[8],
79+
p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
8080
} else {
81-
return NULL;
81+
return "";
8282
}
8383

8484
return str;
@@ -127,7 +127,7 @@ static void update_device_info(esp_bt_gap_cb_param_t *param)
127127
uint8_t eir_len = 0;
128128
esp_bt_gap_dev_prop_t *p;
129129

130-
ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, 18));
130+
ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, sizeof(bda_str)));
131131
for (int i = 0; i < param->disc_res.num_prop; i++) {
132132
p = param->disc_res.prop + i;
133133
switch (p->type) {
@@ -230,18 +230,19 @@ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
230230
p_dev->state == APP_GAP_STATE_SERVICE_DISCOVERING) {
231231
p_dev->state = APP_GAP_STATE_SERVICE_DISCOVER_COMPLETE;
232232
if (param->rmt_srvcs.stat == ESP_BT_STATUS_SUCCESS) {
233-
ESP_LOGI(GAP_TAG, "Services for device %s found", bda2str(p_dev->bda, bda_str, 18));
233+
ESP_LOGI(GAP_TAG, "Services for device %s found", bda2str(p_dev->bda, bda_str, sizeof(bda_str)));
234234
for (int i = 0; i < param->rmt_srvcs.num_uuids; i++) {
235235
esp_bt_uuid_t *u = param->rmt_srvcs.uuid_list + i;
236236
ESP_LOGI(GAP_TAG, "--%s", uuid2str(u, uuid_str, 37));
237237
}
238238
} else {
239-
ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, 18));
239+
ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, sizeof(bda_str)));
240240
}
241241
}
242242
break;
243243
}
244244
case ESP_BT_GAP_RMT_SRVC_REC_EVT:
245+
break;
245246
default: {
246247
ESP_LOGI(GAP_TAG, "event: %d", event);
247248
break;
@@ -255,7 +256,7 @@ static void bt_app_gap_start_up(void)
255256
/* register GAP callback function */
256257
esp_bt_gap_register_callback(bt_app_gap_cb);
257258

258-
char *dev_name = "ESP_GAP_INQRUIY";
259+
char *dev_name = "ESP_GAP_INQUIRY";
259260
esp_bt_gap_set_device_name(dev_name);
260261

261262
/* set discoverable and connectable mode, wait to be connected */

0 commit comments

Comments
 (0)