Skip to content

Commit df47859

Browse files
committed
Merge branch 'fix-string-usage' into 'main'
components: fix unsafe string handling and enable stringop warnings See merge request app-frameworks/esp-matter!1522
2 parents c60f155 + 4db0b04 commit df47859

12 files changed

Lines changed: 49 additions & 18 deletions

File tree

components/esp_matter/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ idf_component_register( SRC_DIRS ${SRC_DIRS_LIST}
104104
idf_build_set_property(COMPILE_OPTIONS "-Wno-error=uninitialized;-Wno-error=maybe-uninitialized;-Wno-missing-field-initializers;" APPEND)
105105
idf_build_set_property(COMPILE_OPTIONS "-Wno-error=array-bounds" APPEND)
106106

107+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wstringop-truncation" "-Wstringop-overflow")
108+
107109
if (CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER)
108110
if (CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
109111
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u data_model_utils_impl")

components/esp_matter/utils/jsontlv/json_to_tlv.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ static esp_err_t parse_json_name(const char *name, element_context &element_ctx,
198198
"Failed to parse json name");
199199
ESP_RETURN_ON_ERROR(internal_convert_tlv_tag(tag_number, element_ctx.tag, implicit_profile_id), TAG,
200200
"Failed to convert TLV tag");
201-
strncpy(element_ctx.json_name, name, strlen(name));
202-
element_ctx.json_name[strlen(name)] = 0;
201+
strlcpy(element_ctx.json_name, name, sizeof(element_ctx.json_name));
203202
return ESP_OK;
204203
}
205204

components/esp_matter_bridge/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ endif()
88
idf_component_register(SRC_DIRS ${src_dirs_list}
99
INCLUDE_DIRS ${include_dirs_list}
1010
REQUIRES esp_matter)
11+
12+
if (CONFIG_ESP_MATTER_ENABLE_DATA_MODEL)
13+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wstringop-truncation" "-Wstringop-overflow")
14+
endif()

components/esp_matter_console/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ idf_component_register(SRC_DIRS ${src_dirs}
1313
INCLUDE_DIRS .
1414
EXCLUDE_SRCS ${exclude_srcs_list}
1515
PRIV_REQUIRES ${priv_req})
16+
17+
if (CONFIG_ENABLE_CHIP_SHELL)
18+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wstringop-truncation" "-Wstringop-overflow")
19+
endif()

components/esp_matter_console/esp_matter_console_otcli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static esp_err_t otcli_handler(int argc, char *argv[])
3737
/* the beginning of command "matter esp otcli" has already been removed */
3838
std::unique_ptr<char[]> cli_str(new char[CLI_INPUT_BUFF_LENGTH]);
3939
memset(cli_str.get(), 0, CLI_INPUT_BUFF_LENGTH);
40-
uint8_t len = 0;
40+
size_t len = 0;
4141
for (size_t i = 0; i < (size_t)argc; ++i) {
4242
len = len + strlen(argv[i]) + 1;
4343
if (len > CLI_INPUT_BUFF_LENGTH - 1) {

components/esp_matter_controller/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ idf_component_register(SRC_DIRS ${src_dirs_list}
4646
REQUIRES ${requires_list})
4747

4848
idf_build_set_property(COMPILE_OPTIONS "-Wno-write-strings" APPEND)
49+
50+
if (CONFIG_ESP_MATTER_CONTROLLER_ENABLE)
51+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wstringop-truncation" "-Wstringop-overflow")
52+
endif()

components/esp_matter_controller/attestation_store/esp_matter_attestation_trust_store.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ static const char *get_filename_extension(const char *filename)
3939

4040
paa_der_cert_iterator::paa_der_cert_iterator(const char *path)
4141
{
42-
strncpy(m_path, path, strnlen(path, 16));
43-
m_path[15] = 0;
42+
if (path == nullptr) {
43+
assert(false);
44+
return;
45+
}
46+
strlcpy(m_path, path, sizeof(m_path));
4447
m_dir = opendir(path);
4548
if (!m_dir) {
4649
ESP_LOGE(TAG, "Failed to open the directory");
@@ -258,8 +261,7 @@ CHIP_ERROR dcl_attestation_trust_store::GetProductAttestationAuthorityCert(const
258261
http_payload.Calloc(2400);
259262
ESP_GOTO_ON_FALSE(http_payload.Get(), ESP_ERR_NO_MEM, close, TAG, "Failed to alloc memory for http_payload");
260263
if (http_status_code == HttpStatus_Ok) {
261-
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize());
262-
http_payload[http_len] = '\0';
264+
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize() - 1);
263265
} else {
264266
ESP_LOGE(TAG, "Status = %d. Invalid response for %s", http_status_code, url);
265267
ret = ESP_FAIL;

components/esp_matter_ota_provider/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ idf_component_register(SRCS "${srcs}"
1313
INCLUDE_DIRS "${include_dirs}"
1414
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
1515
REQUIRES esp_matter esp_http_client json_parser)
16+
17+
if (CONFIG_ESP_MATTER_OTA_PROVIDER_ENABLED)
18+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wstringop-truncation" "-Wstringop-overflow")
19+
endif()

components/esp_matter_ota_provider/include/esp_matter_ota_bdx_sender.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class OtaBdxSender : public chip::bdx::Responder {
4848

4949
void SetOtaImageUrl(const char *otaImageUrl)
5050
{
51-
strncpy(mOtaImageUrl, otaImageUrl, strnlen(otaImageUrl, OTA_URL_MAX_LEN));
51+
strlcpy(mOtaImageUrl, otaImageUrl, sizeof(mOtaImageUrl));
5252
}
5353

5454
const char *GetOtaImageUrl() const

components/esp_matter_ota_provider/src/esp_matter_ota_candidates.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static int _search_ota_candidate_from_cache(uint16_t vendor_id, uint16_t product
9292

9393
static size_t _find_empty_ota_candidates()
9494
{
95-
uint8_t oldest_candidate_lifetime = 0;
95+
uint32_t oldest_candidate_lifetime = 0;
9696
size_t oldest_candidate_index = 0;
9797
for (size_t index = 0; index < max_ota_candidate_count; ++index) {
9898
if (!_ota_candidates_cache[index]) {
@@ -160,11 +160,9 @@ static esp_err_t _query_software_version_array(const uint16_t vendor_id, const u
160160
http_payload.Calloc(1024);
161161
if ((http_len > 0) && (http_status_code == 200)) {
162162
ESP_GOTO_ON_FALSE(http_payload.Get(), ESP_ERR_NO_MEM, close, TAG, "Failed to alloc memory for http_payload");
163-
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize());
164-
http_payload[http_len] = '\0';
163+
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize() - 1);
165164
} else {
166-
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize());
167-
http_payload[http_len] = '\0';
165+
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize() - 1);
168166
ESP_LOGE(TAG, "Invalid response for %s", url);
169167
ESP_LOGE(TAG, "Status = %d, Data = %s", http_status_code, http_len > 0 ? http_payload.Get() : "None");
170168
ret = ESP_FAIL;
@@ -253,11 +251,9 @@ static esp_err_t _query_ota_candidate(model_version_t *model, uint32_t new_softw
253251
http_payload.Calloc(1024);
254252
if ((http_len > 0) && (http_status_code == 200)) {
255253
ESP_GOTO_ON_FALSE(http_payload.Get(), ESP_ERR_NO_MEM, close, TAG, "Failed to alloc memory for http_payload");
256-
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize());
257-
http_payload[http_len] = '\0';
254+
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize() - 1);
258255
} else {
259-
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize());
260-
http_payload[http_len] = '\0';
256+
http_len = esp_http_client_read_response(client, http_payload.Get(), http_payload.AllocatedSize() - 1);
261257
ESP_LOGE(TAG, "Invalid response for %s", url);
262258
ESP_LOGE(TAG, "Status = %d, Data = %s", http_status_code, http_len > 0 ? http_payload.Get() : "None");
263259
ret = ESP_FAIL;
@@ -290,6 +286,9 @@ static esp_err_t _query_ota_candidate(model_version_t *model, uint32_t new_softw
290286
}
291287
if (json_obj_get_strlen(&jctx, "otaUrl", &string_len) == 0 &&
292288
json_obj_get_string(&jctx, "otaUrl", model->ota_url, sizeof(model->ota_url)) == 0) {
289+
string_len = string_len < sizeof(model->ota_url) - 1
290+
? string_len
291+
: sizeof(model->ota_url) - 1;
293292
model->ota_url[string_len] = 0;
294293
}
295294
} else {

0 commit comments

Comments
 (0)