Skip to contentSkip to conversation

block: Fix integer promotion error in bdrv_getlength()

PATCH Applied Tuguoyi 3 participants 6 messages Nov 5, 2020 – Nov 6, 2020

lore
Share & tools
lore

Tuguoyi

author
#0 lore raw

PATCH 1/1 block: Fix integer promotion error in bdrv_getlength()

As BDRV_SECTOR_SIZE is of type uint64_t, the expression will automatically convert the @ret to uint64_t. When an error code returned from bdrv_nb_sectors(), the promoted @ret will be a very large number, as a result the -EFBIG will be returned which is not the real error code.

---
 block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block.c +1 -1
index 430edf7..f14254c 100644
@@ -5082,7 +5082,7 @@ int64_t bdrv_getlength(BlockDriverState *bs)
 {
     int64_t ret = bdrv_nb_sectors(bs);
 
-    ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
+    ret = ret > INT64_MAX / (int64_t)BDRV_SECTOR_SIZE ? -EFBIG : ret;
     return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
 }
 
Signature · 7 lines
2.7.4

Please ignore the [PATCH] block: Return the real error code in bdrv_getlength

--
Best regards,
Guoyi
Applied
#1 lore raw
Quoted 10 lines · Tuguoyi

As BDRV_SECTOR_SIZE is of type uint64_t, the expression will automatically convert the @ret to uint64_t. When an error code returned from bdrv_nb_sectors(), the promoted @ret will be a very large number, as a result the -EFBIG will be returned which is not the real error code.

---
  block.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, applied to my block branch:

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

Max

#2 lore raw
Quoted 7 lines · Max Reitz
Quoted 6 lines · Tuguoyi

As BDRV_SECTOR_SIZE is of type uint64_t, the expression will automatically convert the @ret to uint64_t. When an error code returned from bdrv_nb_sectors(), the promoted @ret will be a very large number, as a result the -EFBIG will be returned which is not the real error code.

Quoted 9 lines
Quoted 4 lines

---
  block.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, applied to my block branch:

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

I actually preferred the v1 solution, rather than this v2, as it avoided a cast.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#3 lore raw
Quoted 13 lines · Eric Blake
Quoted 12 lines · Max Reitz
Quoted 10 lines · Tuguoyi

As BDRV_SECTOR_SIZE is of type uint64_t, the expression will automatically convert the @ret to uint64_t. When an error code returned from bdrv_nb_sectors(), the promoted @ret will be a very large number, as a result the -EFBIG will be returned which is not the real error code.

---
   block.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 7 lines
Quoted 4 lines

Thanks, applied to my block branch:

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

I actually preferred the v1 solution, rather than this v2, as it avoided a cast.

I don’t, because it doesn’t make the ?: go away, so I prefer the less invasive version.

If you want to send your suggested version (that drops both ?:), I’m happy to take that instead.

Max

#4 lore raw
Quoted 8 lines · Max Reitz
Quoted 2 lines

I actually preferred the v1 solution, rather than this v2, as it avoided a cast.

I don’t, because it doesn’t make the ?: go away, so I prefer the less invasive version.

If you want to send your suggested version (that drops both ?:), I’m happy to take that instead.

Will do.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
author
#5 lore raw
In reply to Eric Blake
Quoted 13 lines · Eric Blake
Quoted 12 lines · Max Reitz
Quoted 10 lines · Tuguoyi

As BDRV_SECTOR_SIZE is of type uint64_t, the expression will automatically convert the @ret to uint64_t. When an error code returned from bdrv_nb_sectors(), the promoted @ret will be a very large number, as a result the -EFBIG will be returned which is not the real error code.

---
  block.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 7 lines
Quoted 4 lines

Thanks, applied to my block branch:

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

I actually preferred the v1 solution, rather than this v2, as it avoided a cast.

There are several type promotion bugs(commits '570542ec') found recently, so i think explicitly casting the integer type can give a hint that there is a potential type promotion if you don't do that.

Actually your solution look much simple and clear, and it's ok for me

--
Best regards,
Guoyi