]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bitmap: exclude nbits == 0 cases from bitmap test
authorYury Norov <ynorov@nvidia.com>
Thu, 19 Mar 2026 00:43:47 +0000 (20:43 -0400)
committerYury Norov <ynorov@nvidia.com>
Mon, 23 Mar 2026 17:56:25 +0000 (13:56 -0400)
Bitmap API handles nbits == 0 in most cases correctly, i.e. it doesn't
dereferene underlying bitmap and returns a sane value where convenient,
or implementation defined, or undef.

Implicitly testing nbits == 0 case, however, may make an impression that
this is a regular case. This is wrong. In most cases nbits == 0 is a
sign of an error on a client side. The tests should not make such an
implression.

This patch reworks the existing tests to not test nbits == 0. The
following patch adds an explicit test for it with an appropriate
precaution.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
lib/test_bitmap.c

index eeb497016ed373dd6d4eef803263f3e169b4df8d..b6f27c632c75366f836b2217f98f31f9bdf8919d 100644 (file)
@@ -653,7 +653,7 @@ static void __init test_bitmap_arr32(void)
 
        memset(arr, 0xa5, sizeof(arr));
 
-       for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
+       for (nbits = 1; nbits < EXP1_IN_BITS; ++nbits) {
                bitmap_to_arr32(arr, exp1, nbits);
                bitmap_from_arr32(bmap2, arr, nbits);
                expect_eq_bitmap(bmap2, exp1, nbits);
@@ -681,7 +681,7 @@ static void __init test_bitmap_arr64(void)
 
        memset(arr, 0xa5, sizeof(arr));
 
-       for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
+       for (nbits = 1; nbits < EXP1_IN_BITS; ++nbits) {
                memset(bmap2, 0xff, sizeof(arr));
                bitmap_to_arr64(arr, exp1, nbits);
                bitmap_from_arr64(bmap2, arr, nbits);
@@ -714,7 +714,7 @@ static void noinline __init test_mem_optimisations(void)
        unsigned int start, nbits;
 
        for (start = 0; start < 1024; start += 8) {
-               for (nbits = 0; nbits < 1024 - start; nbits += 8) {
+               for (nbits = 1; nbits < 1024 - start; nbits += 8) {
                        memset(bmap1, 0x5a, sizeof(bmap1));
                        memset(bmap2, 0x5a, sizeof(bmap2));
 
@@ -873,7 +873,7 @@ static void __init test_bitmap_weight(void)
 
        /* Test outline implementation */
        w = bitmap_weight(exp1, EXP1_IN_BITS);
-       for (bit = 0; bit < EXP1_IN_BITS; bit++) {
+       for (bit = 1; bit < EXP1_IN_BITS; bit++) {
                w1 = bitmap_weight(exp1, bit);
                w2 = bitmap_weight_from(exp1, bit, EXP1_IN_BITS);
                expect_eq_uint(w1 + w2, w);