Skip to content

Commit 01ff9d3

Browse files
authored
Fix argument count checks and hex buffer dump formatter handling in BACnet property value shell commands. (#64)
2 parents ff37344 + 471ccfc commit 01ff9d3

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

zephyr/subsys/bacnet_shell/bacnet_shell_property.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int bacnet_shell_object_type_instance_parse(
3535
{
3636
uint32_t found_index = 0;
3737

38-
if (argc < 1) {
38+
if (argc < 2) {
3939
shell_help(sh);
4040
return -EINVAL;
4141
}
@@ -58,7 +58,7 @@ int bacnet_shell_object_type_instance_parse(
5858
if (object_instance) {
5959
*object_instance = Device_Object_Instance_Number();
6060
}
61-
} else if (argc > 1) {
61+
} else if (argc > 2) {
6262
if (!bacnet_string_to_uint32(argv[2], &found_index)) {
6363
shell_error(
6464
sh,
@@ -312,14 +312,20 @@ static int cmd_json_print_hex_dump(
312312
const char *suffix,
313313
const char *append)
314314
{
315-
char str[(buffer_length * 2) + 1];
315+
size_t str_size = ((size_t)buffer_length * 2U) + 1U;
316+
char str[str_size];
316317
int i, s, slen;
317318

318319
s = 0;
319320
for (i = 0; i < buffer_length; i++) {
320-
slen = sprintf(&str[s], "%02x", buffer[i]);
321+
unsigned byte = (unsigned)(unsigned char)buffer[i];
322+
slen = sprintf(&str[s], "%02x", byte);
323+
if (slen != 2) {
324+
break;
325+
}
321326
s += slen;
322327
}
328+
str[s] = '\0';
323329
cmd_json_print_key_value(sh, prefix, key, str, suffix, append);
324330

325331
return 0;
@@ -569,6 +575,7 @@ static int cmd_value(const struct shell *sh, size_t argc, char **argv)
569575
bool skip_print = false;
570576
bool print_all_properties = false;
571577
char property_name[80] = "";
578+
size_t application_data_len = 0;
572579

573580
if ((argc == 2) || (argc == 3)) {
574581
err = bacnet_shell_object_type_instance_parse(
@@ -658,8 +665,10 @@ static int cmd_value(const struct shell *sh, size_t argc, char **argv)
658665
if (apdu_len) {
659666
bacnet_shell_write_property_parameter_init(&wpdata, &rpdata, 0);
660667
wpdata.priority = priority;
661-
wpdata.application_data_len = apdu_len;
662-
memcpy(&wpdata.application_data, apdu, apdu_len);
668+
application_data_len =
669+
min(apdu_len, sizeof(wpdata.application_data));
670+
wpdata.application_data_len = application_data_len;
671+
memcpy(&wpdata.application_data, apdu, application_data_len);
663672
status = Device_Write_Property(&wpdata);
664673
if (status) {
665674
cmd_json_print_property_value_tag(

0 commit comments

Comments
 (0)