Skip to content

Commit db711bd

Browse files
committed
components/esp_matter_console: fix input validation and command dispatch
- fix command search to break outer loop when command found - validate hex prefix (0x) on console attribute get/set arguments - add bounds check for UDC client index
1 parent c75c1a6 commit db711bd

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

components/esp_matter_console/esp_matter_console.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,21 @@ void engine::for_each_command(command_iterator_t *on_command, void *arg)
4848

4949
esp_err_t engine::exec_command(int argc, char *argv[])
5050
{
51-
esp_err_t err = ESP_ERR_INVALID_ARG;
5251
if (argc <= 0) {
53-
return err;
52+
return ESP_ERR_INVALID_ARG;
5453
}
54+
5555
// find the command from the command set
5656
for (unsigned i = 0; i < _command_set_count; ++i) {
5757
for (unsigned j = 0; j < _command_set_size[i]; ++j) {
5858
if (strcmp(argv[0], _command_set[i][j].name) == 0 && _command_set[i][j].handler) {
59-
err = _command_set[i][j].handler(argc - 1, &argv[1]);
60-
break;
59+
return _command_set[i][j].handler(argc - 1, &argv[1]);
6160
}
6261
}
6362
}
64-
return err;
63+
64+
return ESP_ERR_INVALID_ARG;
65+
6566
}
6667

6768
esp_err_t engine::register_commands(const command_t *command_set, unsigned count)

components/esp_matter_console/esp_matter_console_attribute.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ static esp_err_t console_set_handler(int argc, char **argv)
1717
{
1818
VerifyOrReturnError(argc >= 4, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "The arguments for this command is invalid"));
1919

20-
uint16_t endpoint_id = strtoul((const char *)&argv[0][2], NULL, 16);
21-
uint32_t cluster_id = strtoul((const char *)&argv[1][2], NULL, 16);
22-
uint32_t attribute_id = strtoul((const char *)&argv[2][2], NULL, 16);
20+
uint16_t endpoint_id = strtoul(argv[0], NULL, 0);
21+
uint32_t cluster_id = strtoul(argv[1], NULL, 0);
22+
uint32_t attribute_id = strtoul(argv[2], NULL, 0);
2323

2424
attribute_t *attr = attribute::get(endpoint_id, cluster_id, attribute_id);
2525
if (!attr) {
@@ -158,9 +158,9 @@ static esp_err_t console_set_handler(int argc, char **argv)
158158
static esp_err_t console_get_handler(int argc, char **argv)
159159
{
160160
VerifyOrReturnError(argc >= 3, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "The arguments for this command is invalid"));
161-
uint16_t endpoint_id = strtoul((const char *)&argv[0][2], NULL, 16);
162-
uint32_t cluster_id = strtoul((const char *)&argv[1][2], NULL, 16);
163-
uint32_t attribute_id = strtoul((const char *)&argv[2][2], NULL, 16);
161+
uint16_t endpoint_id = strtoul(argv[0], NULL, 0);
162+
uint32_t cluster_id = strtoul(argv[1], NULL, 0);
163+
uint32_t attribute_id = strtoul(argv[2], NULL, 0);
164164

165165
attribute_t *attr = attribute::get(endpoint_id, cluster_id, attribute_id);
166166
if (!attr) {

components/esp_matter_console/esp_matter_console_udc.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static esp_err_t send_udc_request(int argc, char *argv[])
3232
{
3333
ESP_RETURN_ON_FALSE(argc == 2, ESP_ERR_INVALID_ARG, TAG, "Incorrect arguments");
3434
chip::Inet::IPAddress commissioner;
35-
chip::Inet::IPAddress::FromString(argv[0], commissioner);
35+
ESP_RETURN_ON_FALSE(chip::Inet::IPAddress::FromString(argv[0], commissioner),
36+
ESP_ERR_INVALID_ARG, TAG, "Invalid IP address");
3637
uint16_t port = (uint16_t)strtol(argv[1], nullptr, 10);
3738
chip::Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
3839
chip::Server::GetInstance().SendUserDirectedCommissioningRequest(
@@ -44,7 +45,8 @@ static esp_err_t send_udc_cancel(int argc, char *argv[])
4445
{
4546
ESP_RETURN_ON_FALSE(argc == 2, ESP_ERR_INVALID_ARG, TAG, "Incorrect arguments");
4647
chip::Inet::IPAddress commissioner;
47-
chip::Inet::IPAddress::FromString(argv[0], commissioner);
48+
ESP_RETURN_ON_FALSE(chip::Inet::IPAddress::FromString(argv[0], commissioner),
49+
ESP_ERR_INVALID_ARG, TAG, "Invalid IP address");
4850
uint16_t port = (uint16_t)strtol(argv[1], nullptr, 10);
4951
chip::Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
5052
id.SetCancelPasscode(true);

0 commit comments

Comments
 (0)