All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>
Subject: [PULL 11/15] block: Fix integer promotion error in bdrv_getlength()
Date: Mon,  9 Nov 2020 18:38:35 +0100	[thread overview]
Message-ID: <20201109173839.2135984-12-mreitz@redhat.com> (raw)
In-Reply-To: <20201109173839.2135984-1-mreitz@redhat.com>

From: Eric Blake <eblake@redhat.com>

Back in 2015, we attempted to fix error reporting for images that
claimed to have more than INT64_MAX/512 sectors, but due to the type
promotions caused by BDRV_SECTOR_SIZE being unsigned, this
inadvertently forces all negative ret values to be slammed into -EFBIG
rather than the original error.  While we're at it, we can avoid the
confusing ?: by spelling the logic more directly.

Fixes: 4a9c9ea0d3
Reported-by: Guoyi Tu <tu.guoyi@h3c.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20201105155122.60943-1-eblake@redhat.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/block.c b/block.c
index 56bacc9e9f..2fd932154e 100644
--- a/block.c
+++ b/block.c
@@ -5091,8 +5091,13 @@ int64_t bdrv_getlength(BlockDriverState *bs)
 {
     int64_t ret = bdrv_nb_sectors(bs);
 
-    ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
-    return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
+    if (ret < 0) {
+        return ret;
+    }
+    if (ret > INT64_MAX / BDRV_SECTOR_SIZE) {
+        return -EFBIG;
+    }
+    return ret * BDRV_SECTOR_SIZE;
 }
 
 /* return 0 as number of sectors if no device present or error */
-- 
2.28.0



  parent reply	other threads:[~2020-11-09 17:53 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-09 17:38 [PULL 00/15] Block patches for 5.2.0-rc1 Max Reitz
2020-11-09 17:38 ` [PULL 01/15] block: Remove unused include Max Reitz
2020-11-09 17:38 ` [PULL 02/15] block: Move bdrv_drain_all_end_quiesce() to block_int.h Max Reitz
2020-11-09 17:38 ` [PULL 03/15] qcow2: Document and enforce the QCowL2Meta invariants Max Reitz
2020-11-09 17:38 ` [PULL 04/15] hw/block/nvme: fix null ns in register namespace Max Reitz
2020-11-09 17:38 ` [PULL 05/15] hw/block/nvme: fix uint16_t use of uint32_t sgls member Max Reitz
2020-11-09 17:38 ` [PULL 06/15] hw/block/nvme: fix free of array-typed value Max Reitz
2020-11-09 17:38 ` [PULL 07/15] iotests: add filter_qmp_virtio_scsi function Max Reitz
2020-11-09 17:38 ` [PULL 08/15] iotests: rewrite iotest 240 in python Max Reitz
2020-11-09 17:38 ` [PULL 09/15] block: Fixes nfs compiling error on msys2/mingw Max Reitz
2020-11-09 17:38 ` [PULL 10/15] block: enable libnfs on msys2/mingw in cirrus.yml Max Reitz
2020-11-09 17:38 ` Max Reitz [this message]
2020-11-09 17:38 ` [PULL 12/15] block: Fix some code style problems, "foo* bar" should be "foo *bar" Max Reitz
2020-11-09 17:38 ` [PULL 13/15] block: add forgotten bdrv_abort_perm_update() to bdrv_co_invalidate_cache() Max Reitz
2020-11-09 17:38 ` [PULL 14/15] block: add bdrv_replace_node_common() Max Reitz
2020-11-09 17:38 ` [PULL 15/15] block: make bdrv_drop_intermediate() less wrong Max Reitz
2020-11-09 17:49 ` [PULL 00/15] Block patches for 5.2.0-rc1 Max Reitz

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=20201109173839.2135984-12-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=peter.maydell@linaro.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.