From: Andrew Cooper Subject: [PATCH 25/32] block-vvfat: fix memory/handle leaks in commit_one_file() Date: Wed, 22 Apr 2015 16:08:02 +0100 Message-ID: <1429715289-4954-25-git-send-email-andrew.cooper3@citrix.com> References: <1429715235-4759-1-git-send-email-andrew.cooper3@citrix.com> <1429715289-4954-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1429715289-4954-1-git-send-email-andrew.cooper3@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel List-Id: xen-devel@lists.xenproject.org From: Kaifeng Zhu Some handles and memory in commit_one_file are going to be leaked if certain function calls failed. Signed-off-by: Kaifeng Zhu Coverity-IDs: 1055918 1055919 Reviewed-by: Andrew Cooper --- block-vvfat.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/block-vvfat.c b/block-vvfat.c index 345d7be..ec3363c 100644 --- a/block-vvfat.c +++ b/block-vvfat.c @@ -2229,11 +2229,15 @@ static int commit_one_file(BDRVVVFATState* s, if (fd < 0) { fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path, strerror(errno), errno); + qemu_free(cluster); return fd; } if (offset > 0) - if (lseek(fd, offset, SEEK_SET) != offset) + if (lseek(fd, offset, SEEK_SET) != offset) { + close(fd); + qemu_free(cluster); return -3; + } while (offset < size) { uint32_t c1; @@ -2249,11 +2253,17 @@ static int commit_one_file(BDRVVVFATState* s, ret = vvfat_read(s->bs, cluster2sector(s, c), (uint8_t*)cluster, (rest_size + 0x1ff) / 0x200); - if (ret < 0) + if (ret < 0) { + close(fd); + qemu_free(cluster); return ret; + } - if (qemu_write_ok(fd, cluster, rest_size) < 0) + if (qemu_write_ok(fd, cluster, rest_size) < 0) { + close(fd); + qemu_free(cluster); return -2; + } offset += rest_size; c = c1; @@ -2261,6 +2271,7 @@ static int commit_one_file(BDRVVVFATState* s, ftruncate(fd, size); close(fd); + qemu_free(cluster); return commit_mappings(s, first_cluster, dir_index); } -- 1.7.10.4