From patchwork Wed Oct 24 09:50:25 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 193714 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A81D52C0097 for ; Wed, 24 Oct 2012 20:51:29 +1100 (EST) Received: from localhost ([::1]:59332 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TQxcV-0000tL-9f for incoming@patchwork.ozlabs.org; Wed, 24 Oct 2012 05:51:27 -0400 Received: from eggs.gnu.org ([208.118.235.92]:35552) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TQxc7-0000rq-An for qemu-devel@nongnu.org; Wed, 24 Oct 2012 05:51:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TQxc6-0006hZ-5p for qemu-devel@nongnu.org; Wed, 24 Oct 2012 05:51:03 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38585) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TQxc5-0006hN-SP for qemu-devel@nongnu.org; Wed, 24 Oct 2012 05:51:02 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q9O9p0wF003574 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 24 Oct 2012 05:51:00 -0400 Received: from dhcp-5-188.str.redhat.com (dhcp-200-232.str.redhat.com [10.33.200.232]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q9O9ov1O004084; Wed, 24 Oct 2012 05:50:59 -0400 From: Kevin Wolf To: anthony@codemonkey.ws Date: Wed, 24 Oct 2012 11:50:25 +0200 Message-Id: <1351072256-6112-2-git-send-email-kwolf@redhat.com> In-Reply-To: <1351072256-6112-1-git-send-email-kwolf@redhat.com> References: <1351072256-6112-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 01/32] qemu-img: Fix division by zero for zero size images X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Kevin Wolf Reviewed-by: Paolo Bonzini --- qemu-img.c | 23 +++++++++++++++-------- 1 files changed, 15 insertions(+), 8 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index f17f187..849eb41 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -674,7 +674,7 @@ static int img_convert(int argc, char **argv) QEMUOptionParameter *out_baseimg_param; char *options = NULL; const char *snapshot_name = NULL; - float local_progress; + float local_progress = 0; int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ fmt = NULL; @@ -914,8 +914,10 @@ static int img_convert(int argc, char **argv) sector_num = 0; nb_sectors = total_sectors; - local_progress = (float)100 / - (nb_sectors / MIN(nb_sectors, cluster_sectors)); + if (nb_sectors != 0) { + local_progress = (float)100 / + (nb_sectors / MIN(nb_sectors, cluster_sectors)); + } for(;;) { int64_t bs_num; @@ -986,8 +988,10 @@ static int img_convert(int argc, char **argv) sector_num = 0; // total number of sectors converted so far nb_sectors = total_sectors - sector_num; - local_progress = (float)100 / - (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512)); + if (nb_sectors != 0) { + local_progress = (float)100 / + (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512)); + } for(;;) { nb_sectors = total_sectors - sector_num; @@ -1585,7 +1589,7 @@ static int img_rebase(int argc, char **argv) int n; uint8_t * buf_old; uint8_t * buf_new; - float local_progress; + float local_progress = 0; buf_old = qemu_blockalign(bs, IO_BUF_SIZE); buf_new = qemu_blockalign(bs, IO_BUF_SIZE); @@ -1594,8 +1598,11 @@ static int img_rebase(int argc, char **argv) bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors); bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors); - local_progress = (float)100 / - (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); + if (num_sectors != 0) { + local_progress = (float)100 / + (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); + } + for (sector = 0; sector < num_sectors; sector += n) { /* How many sectors can we handle with the next read? */