From a742255f8e73a006ad2b3a2c0e4d844d323a2e4e Mon Sep 17 00:00:00 2001 From: lly Date: Thu, 2 Apr 2020 20:13:08 +0800 Subject: [PATCH] ble_mesh: Fix compile error when -O2 (performance) is chosen --- .../esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c | 3 ++- .../api/core/esp_ble_mesh_provisioning_api.c | 9 ++++++--- components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 2 +- components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 3 ++- .../bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h | 4 ++-- components/bt/esp_ble_mesh/mesh_core/provisioner_main.c | 4 +++- components/bt/esp_ble_mesh/mesh_core/provisioner_main.h | 2 +- components/bt/esp_ble_mesh/mesh_core/proxy_server.c | 4 ++-- components/bt/esp_ble_mesh/mesh_core/settings.c | 6 +++--- .../ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c | 2 +- .../ble_mesh_provisioner/main/ble_mesh_demo_main.c | 2 +- 11 files changed, 24 insertions(+), 17 deletions(-) diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c index 7d30b3a64..0f36723e5 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -268,7 +268,8 @@ esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *nam arg.set_node_name.index = index; memset(arg.set_node_name.name, 0, sizeof(arg.set_node_name.name)); - memcpy(arg.set_node_name.name, name, strlen(name)); + strncpy(arg.set_node_name.name, name, ESP_BLE_MESH_NODE_NAME_MAX_LEN); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } 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 c3dfdc04a..d4d321d2a 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); @@ -150,7 +151,8 @@ esp_err_t esp_ble_mesh_set_unprovisioned_device_name(const char *name) msg.act = BTC_BLE_MESH_ACT_SET_DEVICE_NAME; memset(arg.set_device_name.name, 0, sizeof(arg.set_device_name.name)); - memcpy(arg.set_device_name.name, name, strlen(name)); + strncpy(arg.set_device_name.name, name, ESP_BLE_MESH_DEVICE_NAME_MAX_LEN); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -196,7 +198,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 68ea90a3b..753697d2d 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 @@ -697,7 +697,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 */ } __attribute__((packed)) esp_ble_mesh_node_t; 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 1b8b897ae..62e6cfefd 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 8450c93ba..2f7540e91 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 @@ -103,7 +103,7 @@ typedef union { char string[8]; } input_string; struct ble_mesh_set_device_name_args { - char name[ESP_BLE_MESH_DEVICE_NAME_MAX_LEN]; + char name[ESP_BLE_MESH_DEVICE_NAME_MAX_LEN + 1]; } set_device_name; struct ble_mesh_provisioner_read_oob_pub_key_args { uint8_t link_idx; @@ -157,7 +157,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 f84fcd491..e1d7df4f2 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -490,6 +490,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; } @@ -625,7 +627,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 fcee9fdd9..f5552cd24 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -42,7 +42,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 */ } __packed; 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 676b53388..72e443a90 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 3e62f9fb1..3dfafffa1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -1081,7 +1081,7 @@ static int node_info_set(u16_t addr, bool *exist) static int node_name_set(u16_t addr) { - 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; @@ -2513,7 +2513,7 @@ void bt_mesh_clear_node_info(u16_t unicast_addr) void bt_mesh_store_node_name(struct bt_mesh_node *node) { - 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; @@ -2522,7 +2522,7 @@ void bt_mesh_store_node_name(struct bt_mesh_node *node) return; } - strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE); + strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE + 1); sprintf(name, "mesh/pn/%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 27e93638d..926f96d98 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 d85fcad7f..4be356ad6 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",