virt/kvm: Replace unsafe shift check with __builtin_clzll()
PATCH 1/1 virt/kvm: Replace unsafe shift check with __builtin_clzll()
From: Super User <[email protected]>
The (mask << x >> x) == mask pattern to check for shift overflow is considered undefined behavior by modern compilers and can be optimized away incorrectly.
Replace it with a safe and explicit check using __builtin_clzll() to ensure the left shift does not discard any bits. This avoids UB and is more robust.
Signed-off-by Peng Hao <[email protected]>
--- virt/kvm/dirty_ring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/dirty_ring.c +1 -1
@@ -171,7 +171,7 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,/* Backwards visit, careful about overflows! */ if (delta > -BITS_PER_LONG && delta <0 && - (mask << -delta >> -delta) == mask) { + (unsigned long )-delta <=__builtin_clzll (mask)) { cur_offset = next_offset; mask = (mask << -delta) |1 ;continue ;
Signature · 1 line
2.49.0