Skip to contentSkip to conversation

tools/libs/light: correct bitmap operations

PATCH In review Juergen Gross 4 participants 6 messages Nov 6, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libs/light: correct bitmap operations

Libxl bitmap operations for single bits (test, set, reset) take the bit number as a signed integer without testing the value to be larger than 0.

Correct that by adding the appropriate tests.

---
 tools/libs/light/libxl_utils.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/libs/light/libxl_utils.c +3 -3
index b039143b8a..4699c4a0a3 100644
@@ -688,21 +688,21 @@ int libxl_bitmap_is_empty(const libxl_bitmap *bitmap)
 
 int libxl_bitmap_test(const libxl_bitmap *bitmap, int bit)
 {
-    if (bit >= bitmap->size * 8)
+    if (bit >= bitmap->size * 8 || bit < 0)
         return 0;
     return (bitmap->map[bit / 8] & (1 << (bit & 7))) ? 1 : 0;
 }
 
 void libxl_bitmap_set(libxl_bitmap *bitmap, int bit)
 {
-    if (bit >= bitmap->size * 8)
+    if (bit >= bitmap->size * 8 || bit < 0)
         return;
     bitmap->map[bit / 8] |= 1 << (bit & 7);
 }
 
 void libxl_bitmap_reset(libxl_bitmap *bitmap, int bit)
 {
-    if (bit >= bitmap->size * 8)
+    if (bit >= bitmap->size * 8 || bit < 0)
         return;
     bitmap->map[bit / 8] &= ~(1 << (bit & 7));
 }
Signature · 1 line
2.26.2
#1 lore raw
Quoted 7 lines · Juergen Gross

Libxl bitmap operations for single bits (test, set, reset) take the bit number as a signed integer without testing the value to be larger than 0.

Correct that by adding the appropriate tests.

Wouldn't it be better to convert the parameter types to unsigned int?

Jan

#2 lore raw
Quoted 10 lines · Jan Beulich
Quoted 7 lines · Juergen Gross

Libxl bitmap operations for single bits (test, set, reset) take the bit number as a signed integer without testing the value to be larger than 0.

Correct that by adding the appropriate tests.

Wouldn't it be better to convert the parameter types to unsigned int?

Those are official library interfaces. Can we just change them?

Juergen

#3 lore raw
Quoted 6 lines · Jürgen Groß
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross

Libxl bitmap operations for single bits (test, set, reset) take the bit number as a signed integer without testing the value to be larger than 0.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Correct that by adding the appropriate tests.

Wouldn't it be better to convert the parameter types to unsigned int?

Those are official library interfaces. Can we just change them?

Oh, I didn't expect such helpers to be available to users of the library.

Jan

#4 lore raw
In reply to Jan Beulich
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Juergen Gross

Libxl bitmap operations for single bits (test, set, reset) take the bit number as a signed integer without testing the value to be larger than 0.

Correct that by adding the appropriate tests.

Wouldn't it be better to convert the parameter types to unsigned int?

Yes, except their in the API, so immutable.

(whether they should be in the API is a different question...)

~Andrew