Skip to content

Commit 414bee0

Browse files
committed
Added a test for unknown control sequences, update a couple of minor bugs
1 parent 3262389 commit 414bee0

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

embedded_cli.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,13 @@ const char *embedded_cli_get_history(struct embedded_cli *cli,
146146
if (len == 0)
147147
return NULL;
148148
pos += len + 1;
149-
if (pos == sizeof(cli->history))
149+
if (pos >= sizeof(cli->history))
150150
return NULL;
151151
}
152152

153+
if (cli->history[pos] == '\0')
154+
return NULL;
155+
153156
return &cli->history[pos];
154157
#else
155158
(void)cli;
@@ -168,7 +171,7 @@ static void embedded_cli_extend_history(struct embedded_cli *cli)
168171
if (strcmp(cli->buffer, cli->history) == 0)
169172
return;
170173
memmove(&cli->history[len + 1], &cli->history[0],
171-
sizeof(cli->history) - len + 1);
174+
sizeof(cli->history) - (len + 1));
172175
memcpy(cli->history, cli->buffer, len + 1);
173176
// Make sure it's always nul terminated
174177
cli->history[sizeof(cli->history) - 1] = '\0';

embedded_cli.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const char *embedded_cli_get_line(const struct embedded_cli *cli);
133133

134134
/**
135135
* Parses the internal buffer and returns it as an argc/argc combo
136-
* @return number of values in argv (maximum of EMBEDDED_CLI_MAX_ARGC)
136+
* @return number of values in argv (maximum of EMBEDDED_CLI_MAX_ARGC - 1)
137137
*/
138138
int embedded_cli_argc(struct embedded_cli *cli, char ***argv);
139139

tests/embedded_cli_test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define CTRL_L "\x0c"
1919
#define CTRL_R "\x12"
2020
#define CTRL_U "\x15"
21+
#define CTRL_X "\x18"
2122

2223
static void cli_equals(const struct embedded_cli *cli, const char *line)
2324
{
@@ -214,6 +215,8 @@ static void test_multiple(void)
214215
{"abc" LEFT LEFT CTRL_L "\n", "abc"},
215216
{"abc" CTRL_U "\n", ""},
216217
{"abc" LEFT LEFT CTRL_U "\n", "bc"},
218+
// The check below ensures we ignore unknown control sequences
219+
{CTRL_X " " CTRL_X " " CTRL_X "\n", " "},
217220
{NULL, NULL},
218221
};
219222

0 commit comments

Comments
 (0)