From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49562) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XUSUk-0002Q2-MX for qemu-devel@nongnu.org; Wed, 17 Sep 2014 23:35:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XUSUf-0001MN-Ri for qemu-devel@nongnu.org; Wed, 17 Sep 2014 23:34:58 -0400 From: zhanghailiang Date: Thu, 18 Sep 2014 11:33:42 +0800 Message-ID: <1411011222-5116-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain Subject: [Qemu-devel] [PATCH] qga: Fix possible freed memory accessing List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: mdroth@linux.vnet.ibm.com, armbru@redhat.com, luonengjun@huawei.com, peter.huangpeng@huawei.com, qemu-stable@nongnu.org, lcapitulino@redhat.com, zhanghailiang If readdir_r fails, error_setg_errno will reference the freed pointer *dirpath*. Signed-off-by: zhanghailiang --- qga/commands-posix.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 7eed7f4..3082eae 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -965,7 +965,6 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath, g_free(dirpath); return; } - g_free(dirpath); for (;;) { if (readdir_r(dir, &entry, &result) != 0) { @@ -977,10 +976,12 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath, } if (entry.d_type == DT_LNK) { + char *path; + g_debug(" slave device '%s'", entry.d_name); - dirpath = g_strdup_printf("%s/slaves/%s", syspath, entry.d_name); - build_guest_fsinfo_for_device(dirpath, fs, errp); - g_free(dirpath); + path = g_strdup_printf("%s/slaves/%s", syspath, entry.d_name); + build_guest_fsinfo_for_device(path, fs, errp); + g_free(path); if (*errp) { break; @@ -988,6 +989,7 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath, } } + g_free(dirpath); closedir(dir); } -- 1.7.12.4