selftests: ublk: Use ARRAY_SIZE() macro to improve code
authorAkhilesh Patil <akhilesh@ee.iitb.ac.in>
Sun, 17 Aug 2025 09:36:05 +0000 (15:06 +0530)
committerJens Axboe <axboe@kernel.dk>
Mon, 18 Aug 2025 11:36:29 +0000 (05:36 -0600)
Use ARRAY_SIZE() macro while calculating size of an array to improve
code readability and reduce potential sizing errors.
Implement this suggestion given by spatch tool by running
coccinelle script - scripts/coccinelle/misc/array_size.cocci
Follow ARRAY_SIZE() macro usage pattern in ublk.c introduced by,
commit ec120093180b9 ("selftests: ublk: fix ublk_find_tgt()")
wherever appropriate to maintain consistency.

Signed-off-by: Akhilesh Patil <akhilesh@ee.iitb.ac.in>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/aKGihYui6/Pcijbk@bhairav-test.ee.iitb.ac.in
Signed-off-by: Jens Axboe <axboe@kernel.dk>
tools/testing/selftests/ublk/kublk.c

index 95188065b2e984b51a958c257abd1287e69b6a2d..6512dfbdbce3a82f1202de17319ea593337427e6 100644 (file)
@@ -1400,7 +1400,7 @@ static int cmd_dev_get_features(void)
 
                        if (!((1ULL << i)  & features))
                                continue;
-                       if (i < sizeof(feat_map) / sizeof(feat_map[0]))
+                       if (i < ARRAY_SIZE(feat_map))
                                feat = feat_map[i];
                        else
                                feat = "unknown";
@@ -1477,7 +1477,7 @@ static void __cmd_create_help(char *exe, bool recovery)
        printf("\tdefault: nr_queues=2(max 32), depth=128(max 1024), dev_id=-1(auto allocation)\n");
        printf("\tdefault: nthreads=nr_queues");
 
-       for (i = 0; i < sizeof(tgt_ops_list) / sizeof(tgt_ops_list[0]); i++) {
+       for (i = 0; i < ARRAY_SIZE(tgt_ops_list); i++) {
                const struct ublk_tgt_ops *ops = tgt_ops_list[i];
 
                if (ops->usage)