Skip to content

Commit 871c433

Browse files
rustylifebonzini
authored andcommitted
KVM: use struct_size() and flex_array_size() helpers in kvm_io_bus_unregister_dev()
Make use of the struct_size() helper to avoid any potential type mistakes and protect against potential integer overflows Make use of the flex_array_size() helper to calculate the size of a flexible array member within an enclosing structure Suggested-by: Gustavo A. R. Silva <gustavoars@kernel.org> Signed-off-by: Rustam Kovhaev <rkovhaev@gmail.com> Message-Id: <20200918120500.954436-1-rkovhaev@gmail.com> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 4407a79 commit 871c433

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

virt/kvm/kvm_main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4350,10 +4350,10 @@ void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
43504350
new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
43514351
GFP_KERNEL_ACCOUNT);
43524352
if (new_bus) {
4353-
memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
4353+
memcpy(new_bus, bus, struct_size(bus, range, i));
43544354
new_bus->dev_count--;
43554355
memcpy(new_bus->range + i, bus->range + i + 1,
4356-
(new_bus->dev_count - i) * sizeof(struct kvm_io_range));
4356+
flex_array_size(new_bus, range, new_bus->dev_count - i));
43574357
} else {
43584358
pr_err("kvm: failed to shrink bus, removing it completely\n");
43594359
for (j = 0; j < bus->dev_count; j++) {

0 commit comments

Comments
 (0)