bitmap: assert that start and nr are non negative
PATCH 1/1 bitmap: assert that start and nr are non negative
commit e1123a3b introduced a data corruption regression in the iscsi driver because it passed -1 as nr to bitmap_set and bitmap_clear. Add an assertion to catch such flaws earlier.
Suggested-by Fam Zheng <[email protected]>
Signed-off-by Peter Lieven <[email protected]>
--- util/bitmap.c | 8 ++++++++ 1 file changed, 8 insertions(+)
util/bitmap.c +8 -0
@@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr)int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +while (nr - bits_to_set >=0 ) { *p |= mask_to_set; nr -= bits_to_set;
@@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr)int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +/* First word */ if (nr - bits_to_set >0 ) {atomic_or (p, mask_to_set);
@@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr)int bits_to_clear =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_clear =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +while (nr - bits_to_clear >=0 ) { *p &= ~mask_to_clear; nr -= bits_to_clear;
@@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)unsigned long dirty =0 ;unsigned long old_bits; +assert (start >=0 && nr >=0 ); +/* First word */ if (nr - bits_to_clear >0 ) { old_bits =atomic_fetch_and (p, ~mask_to_clear);
Signature · 1 line
1.9.1
Quoted 54 lines · Peter Lieven
commit
e1123a3bintroduced a data corruption regression in the iscsi driver because it passed -1 as nr tobitmap_setandbitmap_clear. Add an assertion to catch such flaws earlier.Suggested-by Fam Zheng <[email protected]>Signed-off-by Peter Lieven <[email protected]>--- util/bitmap.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/bitmap.c +8
-0@@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr)int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +while (nr - bits_to_set >=0 ) { *p |= mask_to_set; nr -= bits_to_set;@@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr)int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +/* First word */ if (nr - bits_to_set >0 ) {atomic_or (p, mask_to_set);@@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr)int bits_to_clear =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_clear =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +while (nr - bits_to_clear >=0 ) { *p &= ~mask_to_clear; nr -= bits_to_clear;@@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)unsigned long dirty =0 ;unsigned long old_bits; +assert (start >=0 && nr >=0 ); +/* First word */ if (nr - bits_to_clear >0 ) { old_bits =atomic_fetch_and (p, ~mask_to_clear);Signature · 1 line
1.9.1
Reviewed-by Fam Zheng <[email protected]>
Paolo Bonzini
AppliedQuoted 43 lines · Peter Lieven
commit
e1123a3bintroduced a data corruption regression in the iscsi driver because it passed -1 as nr tobitmap_setandbitmap_clear. Add an assertion to catch such flaws earlier.Suggested-by Fam Zheng <[email protected]>Signed-off-by Peter Lieven <[email protected]>--- util/bitmap.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/bitmap.c +6
-0@@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr)int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +while (nr - bits_to_set >=0 ) { *p |= mask_to_set; nr -= bits_to_set;@@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr)int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +/* First word */ if (nr - bits_to_set >0 ) {atomic_or (p, mask_to_set);@@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr)int bits_to_clear =BITS_PER_LONG - (start %BITS_PER_LONG );unsigned long mask_to_clear =BITMAP_FIRST_WORD_MASK (start); +assert (start >=0 && nr >=0 ); +while (nr - bits_to_clear >=0 ) { *p &= ~mask_to_clear; nr -= bits_to_clear;
util/bitmap.c bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr) · line 256
Quoted 8 lines
util/bitmap.c +2
-0@@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)unsigned long dirty =0 ;unsigned long old_bits; +assert (start >=0 && nr >=0 ); +/* First word */ if (nr - bits_to_clear >0 ) { old_bits =atomic_fetch_and (p, ~mask_to_clear);
Queued, thanks.
Paolo