qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: Alberto Garcia <berto@igalia.com>, Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength
Date: Fri, 15 May 2015 16:34:59 +0800	[thread overview]
Message-ID: <20150515083459.GD13255@ad.nay.redhat.com> (raw)
In-Reply-To: <87twventvc.fsf@blackfin.pond.sub.org>

On Fri, 05/15 10:10, Markus Armbruster wrote:
> Fam Zheng <famz@redhat.com> writes:
> 
> > Bogus image may have a large total_sectors that will overflow the
> > multiplication. For cleanness, fix the return code so the error message
> > will be meaningful.
> >
> > Reported-by: Richard W.M. Jones <rjones@redhat.com>
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> > ---
> >  block.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/block.c b/block.c
> > index 7904098..5ee3fdf 100644
> > --- a/block.c
> > +++ b/block.c
> > @@ -2330,6 +2330,7 @@ int64_t bdrv_getlength(BlockDriverState *bs)
> >  {
> >      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;
> >  }
> 
> Signed integer overflow is undefined behavior.  Your code works just
> fine on any remotely sane machine, *except* when the optimizer decides
> to use its undefined behavior license to mess with you.
> 
> A more prudent way to test for overflow would be something like
> 
>     ret > INT64_MAX / BDRV_SECTOR_SIZE

Yes, this is better, will fix.

Thanks,
Fam

  reply	other threads:[~2015-05-15  8:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-15  1:39 [Qemu-devel] [PATCH 0/2] block: Fix error code for bdrv_getlength when the image is too big Fam Zheng
2015-05-15  1:39 ` [Qemu-devel] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength Fam Zheng
2015-05-15  8:04   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2015-05-15  8:10   ` [Qemu-devel] " Markus Armbruster
2015-05-15  8:34     ` Fam Zheng [this message]
2015-05-15  1:39 ` [Qemu-devel] [PATCH 2/2] qemu-iotests: qemu-img info on afl VMDK image with a huge capacity Fam Zheng
2015-05-15  8:06   ` [Qemu-devel] [Qemu-block] " Alberto Garcia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150515083459.GD13255@ad.nay.redhat.com \
    --to=famz@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berto@igalia.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).