Skip to content

Commit baae59b

Browse files
committed
Stpctl derive command list size from array
Switch command list iteration to use sizeof(g_cmd_list) instead of STP_CTL_MAX, preventing potential out-of-bounds access if the enum and list diverge.
1 parent 6be3721 commit baae59b

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

stpctl/stpctl.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
#include "stpctl.h"
1818

19-
#define cmd_max STP_CTL_MAX
2019
CMD_LIST g_cmd_list[] = {
2120
"help", STP_CTL_HELP,
2221
"all", STP_CTL_DUMP_ALL,
@@ -36,20 +35,22 @@ CMD_LIST g_cmd_list[] = {
3635
"mstport", STP_CTL_DUMP_MST_PORT,
3736
};
3837

38+
#define CMD_LIST_COUNT (sizeof(g_cmd_list) / sizeof(g_cmd_list[0]))
39+
3940
void print_cmds()
4041
{
41-
int i;
42+
size_t i;
4243

43-
for (i=0; i<cmd_max; i++)
44+
for (i = 0; i < CMD_LIST_COUNT; i++)
4445
stpout("stpctl %s\n",g_cmd_list[i].cmd_name);
4546
}
4647

4748

4849
int get_cmd_type(char *name)
4950
{
50-
int i;
51+
size_t i;
5152

52-
for (i=0; i<cmd_max; i++)
53+
for (i = 0; i < CMD_LIST_COUNT; i++)
5354
{
5455
if (strcmp(g_cmd_list[i].cmd_name, name) == 0)
5556
return g_cmd_list[i].cmd_type;

0 commit comments

Comments
 (0)