From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:43122) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RMGiw-0000kn-O7 for qemu-devel@nongnu.org; Fri, 04 Nov 2011 06:10:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RMGiq-0003ls-S0 for qemu-devel@nongnu.org; Fri, 04 Nov 2011 06:10:10 -0400 From: Markus Armbruster Date: Fri, 4 Nov 2011 11:10:01 +0100 Message-Id: <1320401401-28871-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PATCH] readline: Fix buffer overrun on re-add to history List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: qemu-trivial@nongnu.org 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 --- readline.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/readline.c b/readline.c index 6a3160a..a6c0039 100644 --- a/readline.c +++ b/readline.c @@ -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) -- 1.7.6.4