Received: from eggs.gnu.org ([2001:4830:134:3::10]:34823) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YtAcM-0006WE-Rd for qemu-devel@nongnu.org; Fri, 15 May 2015 04:05:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YtAcJ-0005TI-Ja for qemu-devel@nongnu.org; Fri, 15 May 2015 04:05:14 -0400 From: Alberto Garcia In-Reply-To: <1431653951-28178-2-git-send-email-famz@redhat.com> References: <1431653951-28178-1-git-send-email-famz@redhat.com> <1431653951-28178-2-git-send-email-famz@redhat.com> Date: Fri, 15 May 2015 10:04:04 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Qemu-devel] [Qemu-block] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org On Fri 15 May 2015 03:39:10 AM CEST, Fam Zheng wrote: > int64_t ret = bdrv_nb_sectors(bs); > > + ret = (int64_t)(ret * BDRV_SECTOR_SIZE) < 0 ? -EFBIG : ret; > return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; Maybe in this case you're safe, but in general there's no guarantee that if there's an overflow the result will be negative. You can do something like this instead: ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret; Of course this is only valid if BDRV_SECTOR_SIZE != 0 ;) Berto