fix sector overflow for scsi disks >1TB large
PATCH 1/1 fix sector overflow for scsi disks >1TB large
Sector numbers can overflow on a virtual scsi disk of over 1TB in size. Qemu's bdrv_read expects an int64_t, so fix the overflow by going to that data type.
Also clip capacity to 2TB instead of returning "capacity modulo 2TB".
hw/scsi-disk.c +4 -1
@@ -50,7 +50,7 @@/* ??? We should probably keep track of whether the data trasfer is a read or a write. Currently we rely on the host getting it right. */ /* Both sector and sector_count are in terms of qemu 512 byte blocks. */ -int sector; +uint64_t sector;int sector_count;/* The amounnt of data in the buffer. */ int buf_len;
@@ -731,6 +731,9 @@/* Returned value is the address of the last sector. */ if (nb_sectors) { nb_sectors--; +/* Clip to 2TB, instead of returning capacity modulo 2TB. */ +if (nb_sectors >UINT_MAX ) + nb_sectors =UINT_MAX ; outbuf[0 ] = (nb_sectors >>24 ) &0xff ; outbuf[1 ] = (nb_sectors >>16 ) &0xff ; outbuf[2 ] = (nb_sectors >>8 ) &0xff ;
hw/scsi-disk.c line 50
Quoted 2 lines · Rik van Riel
hw/scsi-disk.c +0
-0@@ -50,7 +50,7 @@/* ??? We should probably keep track of whether the data trasfer is
Not your fault, but I just noticed a typo: "trasfer" :-)
hw/scsi-disk.c line 739
Quoted 8 lines
hw/scsi-disk.c +3
-0if (nb_sectors) { nb_sectors--; +/* Clip to 2TB, instead of returning capacity modulo 2TB. */ +if (nb_sectors >UINT_MAX ) + nb_sectors =UINT_MAX ; outbuf[0 ] = (nb_sectors >>24 ) &0xff ; outbuf[1 ] = (nb_sectors >>16 ) &0xff ; outbuf[2 ] = (nb_sectors >>8 ) &0xff ;
Wouldn't it be clearer andd safer to say 0xffffffff here, or UINT32_MAX?
I know QEMU only runs on hosts with 32-bit unsigned int, and perhaps that will always be truea, but it's a bit unnecessary to assume it here.
-- JAmie
Quoted 15 lines · Jamie Lokier
hw/scsi-disk.c line 50
Quoted 2 lines · Rik van Riel
hw/scsi-disk.c +0
-0@@ -50,7 +50,7 @@/* ??? We should probably keep track of whether the data trasfer is Not your fault, but I just noticed a typo: "trasfer" :-)
hw/scsi-disk.c line 739
Quoted 8 lines
hw/scsi-disk.c +3
-0if (nb_sectors) { nb_sectors--; +/* Clip to 2TB, instead of returning capacity modulo 2TB. */ +if (nb_sectors >UINT_MAX ) + nb_sectors =UINT_MAX ; outbuf[0 ] = (nb_sectors >>24 ) &0xff ; outbuf[1 ] = (nb_sectors >>16 ) &0xff ; outbuf[2 ] = (nb_sectors >>8 ) &0xff ;
Quoted 4 lines
Wouldn't it be clearer andd safer to say
0xffffffffhere, orUINT32_MAX?I know QEMU only runs on hosts with 32-bit unsigned int, and perhaps that will always be truea, but it's a bit unnecessary to assume it here.
Neither of your suggested changes hurts one bit, so I'll send in a new patch :)
Signature · 1 line
All rights reversed.