Skip to content

Commit 0b51594

Browse files
committed
Merge branch 'backport_node_label/v1.2' into 'release/v1.2'
fix/node-label : [v1.2] cherry-pick node-label read failure issue to release/v1.2. See merge request app-frameworks/esp-matter!630
2 parents afc9efe + 3c451a7 commit 0b51594

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

components/esp_matter/esp_matter_attribute_utils.cpp

+44-30
Original file line numberDiff line numberDiff line change
@@ -1239,42 +1239,56 @@ esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeTyp
12391239
break;
12401240

12411241
case ESP_MATTER_VAL_TYPE_CHAR_STRING:
1242-
if (attribute_type) {
1243-
*attribute_type = ZCL_CHAR_STRING_ATTRIBUTE_TYPE;
1244-
}
1245-
if (attribute_size) {
1246-
*attribute_size = val->val.a.t;
1247-
}
1248-
if (value) {
1249-
int data_size_len = val->val.a.t - val->val.a.s;
1250-
memcpy(value, (uint8_t *)&val->val.a.s, data_size_len);
1251-
if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST)
1252-
{
1253-
ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer."
1254-
"Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST");
1255-
return ESP_FAIL;
1242+
{
1243+
if (attribute_type) {
1244+
*attribute_type = ZCL_CHAR_STRING_ATTRIBUTE_TYPE;
1245+
}
1246+
size_t string_len = strnlen((const char *)val->val.a.b, val->val.a.s);
1247+
size_t data_size_len = val->val.a.t - val->val.a.s;
1248+
if (string_len >= UINT8_MAX || data_size_len != 1) {
1249+
return ESP_ERR_INVALID_ARG;
1250+
}
1251+
uint8_t data_size = string_len;
1252+
if (attribute_size) {
1253+
*attribute_size = string_len + data_size_len;
1254+
}
1255+
if (value) {
1256+
memcpy(value, (uint8_t *)&data_size, data_size_len);
1257+
if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST)
1258+
{
1259+
ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer."
1260+
"Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST");
1261+
return ESP_FAIL;
1262+
}
1263+
memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len));
12561264
}
1257-
memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len));
12581265
}
12591266
break;
12601267

12611268
case ESP_MATTER_VAL_TYPE_LONG_CHAR_STRING:
1262-
if (attribute_type) {
1263-
*attribute_type = ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE;
1264-
}
1265-
if (attribute_size) {
1266-
*attribute_size = val->val.a.t;
1267-
}
1268-
if (value) {
1269-
int data_size_len = val->val.a.t - val->val.a.s;
1270-
memcpy(value, (uint8_t *)&val->val.a.s, data_size_len);
1271-
if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST)
1272-
{
1273-
ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer."
1274-
"Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST");
1275-
return ESP_FAIL;
1269+
{
1270+
if (attribute_type) {
1271+
*attribute_type = ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE;
1272+
}
1273+
size_t string_len = strnlen((const char *)val->val.a.b, val->val.a.s);
1274+
size_t data_size_len = val->val.a.t - val->val.a.s;
1275+
if (string_len >= UINT8_MAX || data_size_len != 2) {
1276+
return ESP_ERR_INVALID_ARG;
1277+
}
1278+
uint16_t data_size = string_len;
1279+
if (attribute_size) {
1280+
*attribute_size = string_len + data_size_len;
1281+
}
1282+
if (value) {
1283+
memcpy(value, (uint8_t *)&data_size, data_size_len);
1284+
if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST)
1285+
{
1286+
ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer."
1287+
"Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST");
1288+
return ESP_FAIL;
1289+
}
1290+
memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len));
12761291
}
1277-
memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len));
12781292
}
12791293
break;
12801294

components/esp_matter/esp_matter_core.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ esp_err_t enable(endpoint_t *endpoint)
607607
attribute::get_data_from_attr_val(&attribute->val, &matter_attributes[attribute_index].attributeType,
608608
&matter_attributes[attribute_index].size, NULL);
609609

610+
/* The length is not fixed for string attribute, so set it to the max size (32) to avoid overflow issue
611+
* when writing a longer string.
612+
*/
613+
if (attribute->val.type == ESP_MATTER_VAL_TYPE_CHAR_STRING ||
614+
attribute->val.type == ESP_MATTER_VAL_TYPE_LONG_CHAR_STRING) {
615+
matter_attributes[attribute_index].size = attribute->val.val.a.s;
616+
}
617+
610618
matter_clusters[cluster_index].clusterSize += matter_attributes[attribute_index].size;
611619
attribute = attribute->next;
612620
attribute_index++;

0 commit comments

Comments
 (0)