From fa716d9842f203f0ed5eab69064e8f27c6192d34 Mon Sep 17 00:00:00 2001 From: Jungo Lin Date: Sun, 10 Mar 2024 10:42:52 +0800 Subject: [PATCH] shell: fix index out bound issue In order to prevent index out of bounds access of the sh->ctx->cmd_buff array when sh->ctx->cmd_buff_pos is 0, it has been added a check for the value of sh->ctx->cmd_buff_pos. This ensures that the array will not be accessed beyond its boundaries. Signed-off-by: Jungo Lin --- subsys/shell/shell.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index 29f9d856d5f..3432731daf7 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -288,8 +288,8 @@ static bool tab_prepare(const struct shell *sh, /* If last command is not completed (followed by space) it is treated * as uncompleted one. */ - int space = isspace((int)sh->ctx->cmd_buff[ - sh->ctx->cmd_buff_pos - 1]); + int space = (sh->ctx->cmd_buff_pos > 0) ? + isspace((int)sh->ctx->cmd_buff[sh->ctx->cmd_buff_pos - 1]) : 0; /* root command completion */ if ((*argc == 0) || ((space == 0) && (*argc == 1))) {