Skip to content

Commit 7fbbd48

Browse files
fix(finsh): reset line state on overflow
1 parent fa0a61c commit 7fbbd48

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

components/finsh/shell.c

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,38 @@ static void shell_push_history(struct finsh_shell *shell)
457457
}
458458
#endif
459459

460+
static void finsh_shell_reset_line(struct finsh_shell *shell)
461+
{
462+
rt_memset(shell->line, 0, sizeof(shell->line));
463+
shell->line_position = 0;
464+
shell->line_curpos = 0;
465+
}
466+
467+
static rt_bool_t finsh_shell_check_line(struct finsh_shell *shell)
468+
{
469+
if ((shell->line_position > FINSH_CMD_SIZE) ||
470+
(shell->line_curpos > shell->line_position))
471+
{
472+
finsh_shell_reset_line(shell);
473+
474+
return RT_FALSE;
475+
}
476+
477+
shell->line[FINSH_CMD_SIZE] = '\0';
478+
479+
return RT_TRUE;
480+
}
481+
482+
static void finsh_shell_update_line_length(struct finsh_shell *shell)
483+
{
484+
rt_size_t length;
485+
486+
shell->line[FINSH_CMD_SIZE] = '\0';
487+
length = rt_strnlen(shell->line, FINSH_CMD_SIZE);
488+
shell->line[length] = '\0';
489+
shell->line_curpos = shell->line_position = (rt_uint16_t)length;
490+
}
491+
460492
#if defined(FINSH_USING_WORD_OPERATION)
461493
static int find_prev_word_start(const char *line, int curpos)
462494
{
@@ -683,6 +715,12 @@ static void finsh_thread_entry(void *parameter)
683715
#if defined(FINSH_USING_FUNC_EXT)
684716
else if (ch >= 0x31 && ch <= 0x34) /* home(0x31), insert(0x32), del(0x33), end(0x34) */
685717
{
718+
if (shell->line_position >= FINSH_CMD_SIZE)
719+
{
720+
finsh_shell_reset_line(shell);
721+
continue;
722+
}
723+
686724
shell->stat = WAIT_EXT_KEY;
687725
shell->line[shell->line_position + 1] = ch; /* store the key code */
688726
continue;
@@ -714,7 +752,8 @@ static void finsh_thread_entry(void *parameter)
714752
else if (key_code == 0x33) /* del key */
715753
{
716754
/* delete character at current cursor position */
717-
if (shell->line_curpos < shell->line_position)
755+
if (finsh_shell_check_line(shell) &&
756+
(shell->line_curpos < shell->line_position))
718757
{
719758
int i;
720759
shell->line_position--;
@@ -745,6 +784,9 @@ static void finsh_thread_entry(void *parameter)
745784
#endif /*defined(FINSH_USING_FUNC_EXT) */
746785
}
747786

787+
if (!finsh_shell_check_line(shell))
788+
continue;
789+
748790
/* received null or error */
749791
if (ch == '\0' || ch == 0xFF) continue;
750792
/* handle tab key */
@@ -758,7 +800,7 @@ static void finsh_thread_entry(void *parameter)
758800
/* auto complete */
759801
shell_auto_complete(&shell->line[0]);
760802
/* re-calculate position */
761-
shell->line_curpos = shell->line_position = (rt_uint16_t)rt_strlen(shell->line);
803+
finsh_shell_update_line_length(shell);
762804

763805
continue;
764806
}
@@ -843,14 +885,16 @@ static void finsh_thread_entry(void *parameter)
843885
msh_exec(shell->line, shell->line_position);
844886

845887
rt_kprintf("%s", FINSH_PROMPT);
846-
rt_memset(shell->line, 0, sizeof(shell->line));
847-
shell->line_curpos = shell->line_position = 0;
888+
finsh_shell_reset_line(shell);
848889
continue;
849890
}
850891

851892
/* it's a large line, discard it */
852893
if (shell->line_position >= FINSH_CMD_SIZE)
853-
shell->line_position = 0;
894+
{
895+
finsh_shell_reset_line(shell);
896+
continue;
897+
}
854898

855899
/* normal character */
856900
if (shell->line_curpos < shell->line_position)
@@ -899,8 +943,7 @@ static void finsh_thread_entry(void *parameter)
899943
if (shell->line_position >= FINSH_CMD_SIZE)
900944
{
901945
/* clear command line */
902-
shell->line_position = 0;
903-
shell->line_curpos = 0;
946+
finsh_shell_reset_line(shell);
904947
}
905948
} /* end of device read */
906949
}

0 commit comments

Comments
 (0)