diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c index 095a369773..684142510c 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c @@ -128,7 +128,8 @@ esp_err_t esp_ble_mesh_node_input_string(const char *string) msg.pid = BTC_PID_PROV; msg.act = BTC_BLE_MESH_ACT_INPUT_STRING; memset(arg.input_string.string, 0, sizeof(arg.input_string.string)); - strncpy(arg.input_string.string, string, strlen(string)); + strncpy(arg.input_string.string, string, + MIN(strlen(string), sizeof(arg.input_string.string))); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); @@ -196,7 +197,8 @@ esp_err_t esp_ble_mesh_provisioner_input_string(const char *string, uint8_t link msg.act = BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR; memset(arg.provisioner_input_str.string, 0, sizeof(arg.provisioner_input_str.string)); - strncpy(arg.provisioner_input_str.string, string, strlen(string)); + strncpy(arg.provisioner_input_str.string, string, + MIN(strlen(string), sizeof(arg.provisioner_input_str.string))); arg.provisioner_input_str.link_idx = link_idx; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index ff908c82b7..1f3fdb66e1 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -699,7 +699,7 @@ typedef struct { uint8_t dev_key[16]; /*!< Node device key */ /* Additional information */ - char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN]; /*!< Node name */ + char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN + 1]; /*!< Node name */ uint16_t comp_length; /*!< Length of Composition Data */ uint8_t *comp_data; /*!< Value of Composition Data */ uint32_t last_hb; /*!< Time (in seconds) when the last heartbeat is received */ diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index 2b2f723444..680120dbef 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -560,7 +560,8 @@ static int btc_ble_mesh_output_string_cb(const char *str) BT_DBG("%s", __func__); - strncpy(mesh_param.node_prov_output_str.string, str, strlen(str)); + strncpy(mesh_param.node_prov_output_str.string, str, + MIN(strlen(str), sizeof(mesh_param.node_prov_output_str.string))); ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT); return (ret == BT_STATUS_SUCCESS) ? 0 : -1; diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h index 57a47b485d..761e0fc035 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -165,7 +165,7 @@ typedef union { } set_primary_elem_addr; struct ble_mesh_provisioner_set_node_name_args { uint16_t index; - char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN]; + char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN + 1]; } set_node_name; struct ble_mesh_provisioner_add_local_app_key_args { uint8_t app_key[16]; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index ffa7fa0316..9d08ab7091 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -572,6 +572,8 @@ int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name) } strncpy(node->name, name, BLE_MESH_NODE_NAME_SIZE); + node->name[BLE_MESH_NODE_NAME_SIZE] = 0; + return 0; } @@ -717,7 +719,7 @@ int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) } } - memset(mesh_nodes[index]->name, 0, BLE_MESH_NODE_NAME_SIZE); + memset(mesh_nodes[index]->name, 0, sizeof(mesh_nodes[index]->name)); strncpy(mesh_nodes[index]->name, name, length); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h index 67eedf1fe3..532c644554 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -38,7 +38,7 @@ struct bt_mesh_node { u8_t dev_key[16]; /* Node device key */ /* Additional information */ - char name[BLE_MESH_NODE_NAME_SIZE]; /* Node name */ + char name[BLE_MESH_NODE_NAME_SIZE + 1]; /* Node name */ u16_t comp_length; /* Length of Composition Data */ u8_t *comp_data; /* Value of Composition Data */ u32_t last_hb; /* Time (in seconds) when the last heartbeat is received */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c index 676b533888..72e443a90b 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c @@ -109,7 +109,7 @@ static enum { MESH_GATT_PROXY, } gatt_svc = MESH_GATT_NONE; -static char device_name[DEVICE_NAME_SIZE] = "ESP-BLE-MESH"; +static char device_name[DEVICE_NAME_SIZE + 1] = "ESP-BLE-MESH"; int bt_mesh_set_device_name(const char *name) { @@ -124,7 +124,7 @@ int bt_mesh_set_device_name(const char *name) } memset(device_name, 0x0, sizeof(device_name)); - memcpy(device_name, name, strlen(name)); + strncpy(device_name, name, DEVICE_NAME_SIZE); return bt_mesh_gatts_set_local_device_name(device_name); } diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index e0f54f62ef..9054986229 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -1121,7 +1121,7 @@ static int node_info_set(u16_t addr, bool prov, bool *exist) static int node_name_set(u16_t addr, bool prov) { - char name[BLE_MESH_NODE_NAME_SIZE] = {0}; + char name[BLE_MESH_NODE_NAME_SIZE + 1] = {0}; char get[16] = {'\0'}; bool exist = false; int err = 0; @@ -2688,7 +2688,7 @@ void bt_mesh_clear_node_info(u16_t unicast_addr, bool prov) void bt_mesh_store_node_name(struct bt_mesh_node *node, bool prov) { - char node_name[BLE_MESH_NODE_NAME_SIZE] = {0}; + char node_name[BLE_MESH_NODE_NAME_SIZE + 1] = {0}; char name[16] = {'\0'}; int err = 0; @@ -2697,7 +2697,7 @@ void bt_mesh_store_node_name(struct bt_mesh_node *node, bool prov) return; } - strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE); + strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE + 1); sprintf(name, prov ? "mesh/pn/%04x/n" : "mesh/sn/%04x/n", node->unicast_addr); err = bt_mesh_save_core_settings(name, (const u8_t *)node_name, BLE_MESH_NODE_NAME_SIZE); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c index 27e93638dc..926f96d984 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c @@ -138,7 +138,7 @@ static void provisioner_prov_complete(int node_index, const uint8_t uuid[16], ui uint8_t elem_num, uint16_t net_idx) { example_node_info_t *node = NULL; - char name[10]; + char name[11] = {0}; esp_err_t err; ESP_LOGI(TAG, "Node index: 0x%x, unicast address: 0x%02x, element num: %d, netkey index: 0x%02x", diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_main.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_main.c index d85fcad7f9..4be356ad69 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_main.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_main.c @@ -191,7 +191,7 @@ static esp_err_t prov_complete(int node_idx, const esp_ble_mesh_octet16_t uuid, esp_ble_mesh_client_common_param_t common = {0}; esp_ble_mesh_cfg_client_get_state_t get_state = {0}; esp_ble_mesh_node_info_t *node = NULL; - char name[10]; + char name[11] = {0}; int err; ESP_LOGI(TAG, "node index: 0x%x, unicast address: 0x%02x, element num: %d, netkey index: 0x%02x",