qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] block: Fix integer promotion error in bdrv_getlength()
@ 2020-11-05 15:51 Eric Blake
  2020-11-05 15:54 ` Alberto Garcia
  2020-11-05 16:32 ` Max Reitz
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Blake @ 2020-11-05 15:51 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, chengchiwen, berto, tu.guoyi, open list:Block layer core,
	armbru, mreitz, wang.yongD

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>
---
 block.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/block.c b/block.c
index 56bacc9e9f13..2fd932154e12 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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] block: Fix integer promotion error in bdrv_getlength()
  2020-11-05 15:51 [PATCH v4] block: Fix integer promotion error in bdrv_getlength() Eric Blake
@ 2020-11-05 15:54 ` Alberto Garcia
  2020-11-05 16:32 ` Max Reitz
  1 sibling, 0 replies; 3+ messages in thread
From: Alberto Garcia @ 2020-11-05 15:54 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: kwolf, chengchiwen, tu.guoyi, open list:Block layer core, armbru,
	mreitz, wang.yongD

On Thu 05 Nov 2020 04:51:22 PM CET, Eric Blake wrote:
> 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>

Reviewed-by: Alberto Garcia <berto@igalia.com>

Berto


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] block: Fix integer promotion error in bdrv_getlength()
  2020-11-05 15:51 [PATCH v4] block: Fix integer promotion error in bdrv_getlength() Eric Blake
  2020-11-05 15:54 ` Alberto Garcia
@ 2020-11-05 16:32 ` Max Reitz
  1 sibling, 0 replies; 3+ messages in thread
From: Max Reitz @ 2020-11-05 16:32 UTC (permalink / raw)
  To: Eric Blake, qemu-devel
  Cc: kwolf, chengchiwen, berto, tu.guoyi, open list:Block layer core,
	armbru, wang.yongD

On 05.11.20 16:51, Eric Blake wrote:
> 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>
> ---
>   block.c | 9 +++++++--
>   1 file changed, 7 insertions(+), 2 deletions(-)

Thanks, applied to my block branch (replacing the original patch):

https://git.xanclic.moe/XanClic/qemu/commits/branch/block

Max



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-11-05 16:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-05 15:51 [PATCH v4] block: Fix integer promotion error in bdrv_getlength() Eric Blake
2020-11-05 15:54 ` Alberto Garcia
2020-11-05 16:32 ` Max Reitz

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).