KVM: Fix off by one in kvm_for_each_vcpu iteration
authorJeff Mahoney <jeffm@suse.com>
Wed, 13 Apr 2011 01:30:17 +0000 (21:30 -0400)
committerAvi Kivity <avi@redhat.com>
Wed, 11 May 2011 11:57:09 +0000 (07:57 -0400)
commitb42fc3cbc3d6e284463e93896679379443e19d56
treec0f08bb19a572b429482b61d881cd4ba6acf4fad
parent71f9833bb1cba9939245f3e57388d87d69f8f399
KVM: Fix off by one in kvm_for_each_vcpu iteration

This patch avoids gcc issuing the following warning when KVM_MAX_VCPUS=1:
warning: array subscript is above array bounds

kvm_for_each_vcpu currently checks to see if the index for the vcpu is
valid /after/ loading it. We don't run into problems because the address
is still inside the enclosing struct kvm and we never deference or write
to it, so this isn't a security issue.

The warning occurs when KVM_MAX_VCPUS=1 because the increment portion of
the loop will *always* cause the loop to load an invalid location since
++idx will always be > 0.

This patch moves the load so that the check occurs before the load and
we don't run into the compiler warning.

Signed-off-by: Neil Brown <neilb@suse.de>
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
include/linux/kvm_host.h