readline: Fix possible array index out of bounds in readline_hist_add()
authorAlex Chen <alex.chen@huawei.com>
Thu, 3 Dec 2020 13:50:43 +0000 (13:50 +0000)
committerStefan Hajnoczi <stefanha@redhat.com>
Mon, 4 Jan 2021 11:13:39 +0000 (11:13 +0000)
When the 'cmdline' is the last entry in 'rs->history' array, there is
no need to put this entry to the end of the array, partly because it is
the last entry, and partly because the next operition will lead to array
index out of bounds.

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Alex Chen <alex.chen@huawei.com>
Message-id: 20201203135043.117072-1-alex.chen@huawei.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
util/readline.c

index e534460da68383db53c86e4d3afc14a5a89b2894..f1ac6e476940446f869bf1b354504628b8484ae5 100644 (file)
@@ -240,6 +240,9 @@ static void readline_hist_add(ReadLineState *rs, const char *cmdline)
         }
         if (strcmp(hist_entry, cmdline) == 0) {
         same_entry:
+            if (idx == READLINE_MAX_CMDS - 1) {
+                return;
+            }
             new_entry = hist_entry;
             /* Put this entry at the end of history */
             memmove(&rs->history[idx], &rs->history[idx + 1],