From 4674bfe77ce549ee8d0b808accbb2bf21c34221e Mon Sep 17 00:00:00 2001
From: Markus Armbruster <armbru@redhat.com>
Date: Mon, 28 Nov 2011 14:09:06 +0100
Subject: [PATCH 10/11] monitor: Fix file_completion() to check for stat()
 failure

RH-Author: Markus Armbruster <armbru@redhat.com>
Message-id: <1322489346-17342-1-git-send-email-armbru@redhat.com>
Patchwork-id: 35484
O-Subject: [RHEL-6.3 PATCH qemu-kvm] monitor: Fix file_completion() to check for stat() failure
Bugzilla: 757713
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
RH-Acked-by: Paul Moore <pmoore@redhat.com>

stat() can fail for a file name just read with readdir().  Easiest way
to trigger is a dangling symbolic link --- look ma, no race!  When it
fails, file_completion() uses sb.st_mode uninitialized.  If the
directory bit happens to be set, it appends a "/" to the completed
name.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
(cherry picked from commit c951d9a6751576a076ac80a5e5145ceb8d794d38)
---
Bug 757713
brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=3853713

 monitor.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 monitor.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/monitor.c b/monitor.c
index 67762ad..74bb5e3 100644
--- a/monitor.c
+++ b/monitor.c
@@ -4217,9 +4217,9 @@ static void file_completion(const char *input)
             /* stat the file to find out if it's a directory.
              * In that case add a slash to speed up typing long paths
              */
-            stat(file, &sb);
-            if(S_ISDIR(sb.st_mode))
+            if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
                 pstrcat(file, sizeof(file), "/");
+            }
             readline_add_completion(cur_mon->rs, file);
         }
     }
-- 
1.7.7.3

