]> xenbits.xensource.com Git - qemu-upstream-4.6-testing.git/commitdiff
readline: Fix buffer overrun on re-add to history
authorMarkus Armbruster <armbru@redhat.com>
Fri, 4 Nov 2011 10:10:01 +0000 (11:10 +0100)
committerStefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Mon, 7 Nov 2011 08:03:49 +0000 (08:03 +0000)
readline_hist_add() moves the history entry to the end of history.  It
uses memmove() to move rs->history[idx + 1..] to rs->history[idx..].
However, its size argument is off by two array elements, so it writes
one element beyond rs->history[], and reads two.

On my system, this clobbers rs->hist_entry and the hole right after
it.  Since the function assigns to rs->hist_entry in time, the bug has
no ill effects for me.

Spotted by Coverity.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
readline.c

index 6a3160aba53058f072d780f7bdbc241cff417361..a6c0039ad2c23bae5817f7ac37f1b1b73487800f 100644 (file)
@@ -236,7 +236,7 @@ static void readline_hist_add(ReadLineState *rs, const char *cmdline)
            new_entry = hist_entry;
            /* Put this entry at the end of history */
            memmove(&rs->history[idx], &rs->history[idx + 1],
-                   (READLINE_MAX_CMDS - idx + 1) * sizeof(char *));
+                   (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
            rs->history[READLINE_MAX_CMDS - 1] = NULL;
            for (; idx < READLINE_MAX_CMDS; idx++) {
                if (rs->history[idx] == NULL)