diff mbox series

KVM: replace ternary operator with min()

Message ID 20221101072513.7364-1-liubo03@inspur.com (mailing list archive)
State New, archived
Headers show
Series KVM: replace ternary operator with min() | expand

Commit Message

Bo Liu Nov. 1, 2022, 7:25 a.m. UTC
Fix the following coccicheck warning:

virt/kvm/kvm_main.c:5289:10-11: WARNING opportunity for min()
virt/kvm/kvm_main.c:5218:10-11: WARNING opportunity for min()

min() macro is defined in include/linux/minmax.h. It avoids
multiple evaluations of the arguments when non-constant and
performs strict type-checking.

Signed-off-by: Bo Liu <liubo03@inspur.com>
---
 virt/kvm/kvm_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

David Laight Nov. 1, 2022, 11:16 a.m. UTC | #1
From: Bo Liu
> Sent: 01 November 2022 07:25
> 
> Fix the following coccicheck warning:
> 
> virt/kvm/kvm_main.c:5289:10-11: WARNING opportunity for min()
...
> -	return r < 0 ? r : 0;
> +	return min(r, 0);

As has been repeatedly pointer out this is bogus.
min() should only be used for 'sizes'.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 3f383f27d3d7..45a56a9600e1 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -5215,7 +5215,7 @@  int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
 	if (!bus)
 		return -ENOMEM;
 	r = __kvm_io_bus_write(vcpu, bus, &range, val);
-	return r < 0 ? r : 0;
+	return min(r, 0);
 }
 EXPORT_SYMBOL_GPL(kvm_io_bus_write);
 
@@ -5286,7 +5286,7 @@  int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
 	if (!bus)
 		return -ENOMEM;
 	r = __kvm_io_bus_read(vcpu, bus, &range, val);
-	return r < 0 ? r : 0;
+	return min(r, 0);
 }
 
 /* Caller must hold slots_lock. */