diff --git a/components/esp_matter/Kconfig b/components/esp_matter/Kconfig index a6cc757f5..37d12c06c 100644 --- a/components/esp_matter/Kconfig +++ b/components/esp_matter/Kconfig @@ -7,6 +7,13 @@ menu "ESP Matter" help The maximum device type count supported per endpoint. + config ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST + int "Largest attribute buffer size" + default 259 + help + The Largest attribute size required for various attributes, the buffer will be used + for reading or writing attributes. + config ESP_MATTER_NVS_PART_NAME string "ESP Matter NVS partition name" default "nvs" diff --git a/components/esp_matter/esp_matter_attribute_utils.cpp b/components/esp_matter/esp_matter_attribute_utils.cpp index fe1969d3c..72fe6bf78 100644 --- a/components/esp_matter/esp_matter_attribute_utils.cpp +++ b/components/esp_matter/esp_matter_attribute_utils.cpp @@ -1106,21 +1106,40 @@ esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeTyp if (value) { int data_size_len = val->val.a.t - val->val.a.s; memcpy(value, (uint8_t *)&val->val.a.s, data_size_len); + if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) + { + ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." + "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); + return ESP_FAIL; + } memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); } break; case ESP_MATTER_VAL_TYPE_CHAR_STRING: - if (attribute_type) { - *attribute_type = ZCL_CHAR_STRING_ATTRIBUTE_TYPE; - } - if (attribute_size) { - *attribute_size = val->val.a.t; - } - if (value) { - int data_size_len = val->val.a.t - val->val.a.s; - memcpy(value, (uint8_t *)&val->val.a.s, data_size_len); - memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); + { + if (attribute_type) { + *attribute_type = ZCL_CHAR_STRING_ATTRIBUTE_TYPE; + } + size_t string_len = strnlen((const char *)val->val.a.b, val->val.a.s); + size_t data_size_len = val->val.a.t - val->val.a.s; + if (string_len >= UINT8_MAX || data_size_len != 1) { + return ESP_ERR_INVALID_ARG; + } + uint8_t data_size = string_len; + if (attribute_size) { + *attribute_size = string_len + data_size_len; + } + if (value) { + memcpy(value, (uint8_t *)&data_size, data_size_len); + if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) + { + ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." + "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); + return ESP_FAIL; + } + memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); + } } break; @@ -1134,6 +1153,12 @@ esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeTyp if (value) { int data_size_len = val->val.a.t - val->val.a.s; memcpy(value, (uint8_t *)&val->val.a.s, data_size_len); + if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) + { + ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." + "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); + return ESP_FAIL; + } memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); } break; diff --git a/components/esp_matter/esp_matter_core.cpp b/components/esp_matter/esp_matter_core.cpp index b8330de09..191284f4c 100644 --- a/components/esp_matter/esp_matter_core.cpp +++ b/components/esp_matter/esp_matter_core.cpp @@ -602,6 +602,13 @@ esp_err_t enable(endpoint_t *endpoint) attribute::get_data_from_attr_val(&attribute->val, &matter_attributes[attribute_index].attributeType, &matter_attributes[attribute_index].size, NULL); + /* The length is not fixed for string attribute, so set it to the max size (32) to avoid overflow issue + * when writing a longer string. + */ + if (attribute->val.type == ESP_MATTER_VAL_TYPE_CHAR_STRING) { + matter_attributes[attribute_index].size = attribute->val.val.a.s; + } + matter_clusters[cluster_index].clusterSize += matter_attributes[attribute_index].size; attribute = attribute->next; attribute_index++; diff --git a/components/esp_matter/zap_common/zap-generated/endpoint_config.h b/components/esp_matter/zap_common/zap-generated/endpoint_config.h index caafa958e..2fbac3d40 100644 --- a/components/esp_matter/zap_common/zap-generated/endpoint_config.h +++ b/components/esp_matter/zap_common/zap-generated/endpoint_config.h @@ -35,7 +35,7 @@ #define ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT 0 // Largest attribute size is needed for various buffers -#define ATTRIBUTE_LARGEST (259) +#define ATTRIBUTE_LARGEST CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST static_assert(ATTRIBUTE_LARGEST <= CHIP_CONFIG_MAX_ATTRIBUTE_STORE_ELEMENT_SIZE, "ATTRIBUTE_LARGEST larger than expected");