[libvirt] [PATCH] qemu: fix an off-by-one error in qemuDomainGetPercpuStats

Eric Blake eblake at redhat.com
Wed Feb 20 15:25:44 UTC 2013


On 02/20/2013 04:51 AM, Guannan Ren wrote:
> The max value of number of cpus to compute(id) should not
> be equal or greater than max cpu number.
> The bug ocurrs when id value is equal to max cpu number which

s/ocurrs/occurs/

> leads to the off-by-one error in the following for loop.
> 
>  # virsh  cpu-stats guest --start 1
>  error: Failed to virDomainGetCPUStats()
> 
>  error: internal error cpuacct parse error
> ---
>  src/qemu/qemu_driver.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)


>      /* number of cpus to compute */
> -    id = max_id;
> -
> -    if (max_id - start_cpu > ncpus - 1)

The old code was trying to avoid the possibility of integer overflow, by
only using subtraction (src/libvirt.c already ensured we have
non-negative values, but not necessarily that the sum of two positive
values won't wrap around to negative).

> +    if ((start_cpu + ncpus) >= max_id)

The new code is not as careful, and has redundant ().

ACK if you change this to the equivalent:

if (start_cpu >= max_id - ncpus)

> +        id = max_id - 1;
> +    else
>          id = start_cpu + ncpus - 1;
>  
>      for (i = 0; i <= id; i++) {
> 

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 621 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20130220/b384f725/attachment-0001.sig>


More information about the libvir-list mailing list