Skip to contentSkip to conversation

chardev/char-mux: tweak mux bitset operations

PATCH Superseded Roman Penyaev 3 participants 5 messages Nov 2, 2024 – Nov 4, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover chardev/char-mux: tweak mux bitset operations

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset operations.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

 chardev/char-mux.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
Cc "Marc-André Lureau" <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Clément Mathieu--Drif <[email protected]>
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 chardev/char-mux: shift unsigned long to avoid 32-bit overflow

Allthough the size of MAX_MUX is equal to 4 and likely will never change, this patch changes type of constant to unsigned long to be on the safe side.

Also add a static compile check that MAX_MUX never bigger than sizeof(d->mux_bitset).

Cc "Marc-André Lureau" <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Clément Mathieu--Drif <[email protected]>
---
 chardev/char-mux.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
chardev/char-mux.c +4 -2
index bda5c45e6058..355520714979 100644
@@ -316,6 +316,8 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
 {
     unsigned int bit;
 
+    QEMU_BUILD_BUG_ON(MAX_MUX > sizeof(d->mux_bitset));
+
     bit = find_next_zero_bit(&d->mux_bitset, MAX_MUX, 0);
     if (bit >= MAX_MUX) {
         error_setg(errp,
@@ -325,7 +327,7 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
         return false;
     }
 
-    d->mux_bitset |= (1 << bit);
+    d->mux_bitset |= (1ul << bit);
     d->backends[bit] = b;
     *tag = bit;
 
@@ -341,7 +343,7 @@ bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag)
         return false;
     }
 
-    d->mux_bitset &= ~(1 << bit);
+    d->mux_bitset &= ~(1ul << bit);
     d->backends[bit] = NULL;
 
     return true;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 chardev/char-mux: make boolean bit check instead of find_next_bit()

In reply to Roman Penyaev

This patch simplifies (and makes less confusing) bit checks by replacing find_next_bit() calls with boolean AND operation.

Resolves: Coverity CID 1563776

Cc "Marc-André Lureau" <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Clément Mathieu--Drif <[email protected]>
---
 chardev/char-mux.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
chardev/char-mux.c +4 -7
index 355520714979..f613a22e3758 100644
@@ -336,15 +336,12 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
 
 bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag)
 {
-    unsigned int bit;
-
-    bit = find_next_bit(&d->mux_bitset, MAX_MUX, tag);
-    if (bit != tag) {
+    if (!(d->mux_bitset & (1ul << tag))) {
         return false;
     }
 
-    d->mux_bitset &= ~(1ul << bit);
-    d->backends[bit] = NULL;
+    d->mux_bitset &= ~(1ul << tag);
+    d->backends[tag] = NULL;
 
     return true;
 }
@@ -353,7 +350,7 @@ void mux_set_focus(Chardev *chr, unsigned int focus)
 {
     MuxChardev *d = MUX_CHARDEV(chr);
 
-    assert(find_next_bit(&d->mux_bitset, MAX_MUX, focus) == focus);
+    assert(d->mux_bitset & (1ul << focus));
 
     if (d->focus != -1) {
         mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Signature · 1 line
2.34.1
Reviewed-by on the v1 series
#3 lore raw
In reply to Roman Penyaev

Hi

Quoted 7 lines · Roman Penyaev

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset operations.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

Quoted 8 lines
 chardev/char-mux.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
Cc "Marc-André Lureau" <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Clément Mathieu--Drif <[email protected]>
Signature · 1 line
Marc-André Lureau
#4 lore raw
In reply to Roman Penyaev

Hi Roman,

Reviewed-by Clément Mathieu--Drif <[email protected]><mailto:[email protected]> Thanks!

On 02/11/2024 12:07, Roman Penyaev wrote:

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset operations.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

 chardev/char-mux.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

--
2.34.1