[libvirt PATCH v2 2/2] storage_file: add support to probe cluster_size from QCOW2 images

John Ferlan jferlan at redhat.com
Sat May 22 13:23:38 UTC 2021



On 5/20/21 11:14 AM, Pavel Hrdina wrote:
>>From QEMU docs/interop/qcow2.txt :
> 
>     Byte  20 - 23:   cluster_bits
>                      Number of bits that are used for addressing an offset
>                      within a cluster (1 << cluster_bits is the cluster size).
> 
> With this patch libvirt will be able to report the current cluster_size
> for all existing storage volumes managed by storage driver.
> 
> Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
> ---
> 
> Changes in v2:
>      - Reworkded to use callback.
> 
>   src/storage/storage_util.c            |  3 ++
>   src/storage_file/storage_file_probe.c | 70 ++++++++++++++++++++-------
>   2 files changed, 56 insertions(+), 17 deletions(-)
> 

[...]

>   
> +static unsigned long long
> +qcow2GetClusterSize(const char *buf,
> +                    size_t buf_size,
> +                    int endian)
> +{
> +    int clusterBits = 0;
> +
> +    if ((QCOWX_HDR_CLUSTER_BITS_OFFSET + 4) > buf_size)
> +        return 0;
> +
> +    if (endian == LV_LITTLE_ENDIAN)
> +        clusterBits = virReadBufInt32LE(buf + QCOWX_HDR_CLUSTER_BITS_OFFSET);
> +    else
> +        clusterBits = virReadBufInt32BE(buf + QCOWX_HDR_CLUSTER_BITS_OFFSET);
> +
> +    if (clusterBits > 0)
> +        return 1 << clusterBits;
> +

Coverity showed me a new error today: OVERFLOW_BEFORE_WIDEN

1) Event overflow_before_widen:	Potentially overflowing expression "1 << 
clusterBits" with type "int" (32 bits, signed) is evaluated using 32-bit 
arithmetic, and then used in a context that expects an expression of 
type "unsigned long long" (64 bits, unsigned).
(2) Event remediation:	To avoid overflow, cast "1" to type "unsigned 
long long".


John


> +    return 0;
> +}
> +
> +

[...]




More information about the libvir-list mailing list