]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: use ARRAY_SIZE() for iteration
authorEmil Velikov <emil.l.velikov@gmail.com>
Fri, 13 Jun 2025 18:48:18 +0000 (19:48 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 16 Jun 2025 12:37:42 +0000 (07:37 -0500)
Currently we use a mix of ARRAY_SIZE() and iterator pointer, where the
latter needs an extra instance for the NULL sentinel. Plus as evidenced
by the EXIT_SUCCESS -> EXIT_FAILURE changes in test-new-module - is
error prone.

Consistently use ARRAY_SIZE(), fixing the logical error in the test
which was flagged by ASan as memory leak :-)

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/371
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
testsuite/test-blacklist.c
testsuite/test-new-module.c
testsuite/test-util.c
testsuite/test-weakdep.c

index 0f7baab96a76347c023f064fd3cb0ab87874a37c..883e0e9d7a634bee8bf9e733e203828318dd8535 100644 (file)
@@ -30,15 +30,14 @@ static int blacklist_1(void)
        int err;
        size_t len = 0;
 
-       static const char *const names[] = { "pcspkr", "pcspkr2", "floppy", "ext4", NULL};
-       const char **name;
+       static const char *const names[] = { "pcspkr", "pcspkr2", "floppy", "ext4" };
 
        ctx = kmod_new(NULL, NULL);
        if (ctx == NULL)
                exit(EXIT_FAILURE);
 
-       for (name = names; *name; name++) {
-               err = kmod_module_new_from_name(ctx, *name, &mod);
+       for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
+               err = kmod_module_new_from_name(ctx, names[i], &mod);
                if (err < 0)
                        goto fail_lookup;
                list = kmod_list_append(list, mod);
index be6115269a6c3452ecc10f3b5b5123961bab9ab8..238908d417d2b872795e7b5979287bdd3296651d 100644 (file)
@@ -23,10 +23,8 @@ static int from_name(void)
                "snd-hda-intel",
                "snd-timer",
                "iTCO_wdt",
-               NULL,
                // clang-format on
        };
-       const char *const *p;
        struct kmod_ctx *ctx;
        struct kmod_module *mod;
        const char *null_config = NULL;
@@ -36,10 +34,10 @@ static int from_name(void)
        if (ctx == NULL)
                exit(EXIT_FAILURE);
 
-       for (p = modnames; p != NULL; p++) {
-               err = kmod_module_new_from_name(ctx, *p, &mod);
+       for (size_t i = 0; i < ARRAY_SIZE(modnames); i++) {
+               err = kmod_module_new_from_name(ctx, modnames[i], &mod);
                if (err < 0)
-                       exit(EXIT_SUCCESS);
+                       exit(EXIT_FAILURE);
 
                printf("modname: %s\n", kmod_module_get_name(mod));
                kmod_module_unref(mod);
@@ -62,9 +60,7 @@ static int from_alias(void)
 {
        static const char *const modnames[] = {
                "ext4.*",
-               NULL,
        };
-       const char *const *p;
        struct kmod_ctx *ctx;
        int err;
 
@@ -72,12 +68,12 @@ static int from_alias(void)
        if (ctx == NULL)
                exit(EXIT_FAILURE);
 
-       for (p = modnames; p != NULL; p++) {
+       for (size_t i = 0; i < ARRAY_SIZE(modnames); i++) {
                struct kmod_list *l, *list = NULL;
 
-               err = kmod_module_new_from_lookup(ctx, *p, &list);
+               err = kmod_module_new_from_lookup(ctx, modnames[i], &list);
                if (err < 0)
-                       exit(EXIT_SUCCESS);
+                       exit(EXIT_FAILURE);
 
                kmod_list_foreach(l, list) {
                        struct kmod_module *m;
index aa47040045d60ec2ffb8cafaf9bd803837de5d02..0421f5924ae8ada2ec3c5467c38ee0d33fb8341e 100644 (file)
 
 static int alias_1(void)
 {
-       static const char *const input[] = {
+       static const char *const aliases[] = {
                // clang-format off
                "test1234",
                "test[abcfoobar]2211",
                "bar[aaa][bbbb]sss",
                "kmod[p.b]lib",
                "[az]1234[AZ]",
-               NULL,
                // clang-format on
        };
 
        char buf[PATH_MAX];
        size_t len;
-       const char *const *alias;
 
-       for (alias = input; *alias != NULL; alias++) {
+       for (size_t i = 0; i < ARRAY_SIZE(aliases); i++) {
                int ret;
 
-               ret = alias_normalize(*alias, buf, &len);
-               printf("input   %s\n", *alias);
+               ret = alias_normalize(aliases[i], buf, &len);
+               printf("input   %s\n", aliases[i]);
                printf("return  %d\n", ret);
 
                if (ret == 0) {
@@ -135,6 +133,7 @@ static int test_path_ends_with_kmod_ext(void)
                const char *val;
                bool res;
        } teststr[] = {
+               // clang-format off
                { "/bla.ko", true },
 #if ENABLE_ZLIB
                { "/bla.ko.gz", true },
@@ -149,12 +148,13 @@ static int test_path_ends_with_kmod_ext(void)
                { "/bla.ko.", false },
                { "/bla.koz", false },
                { "/b", false },
-               { },
-       }, *iter;
+               // clang-format on
+       };
 
-       for (iter = &teststr[0]; iter->val != NULL; iter++) {
-               assert_return(path_ends_with_kmod_ext(iter->val, strlen(iter->val)) ==
-                                     iter->res,
+       for (size_t i = 0; i < ARRAY_SIZE(teststr); i++) {
+               assert_return(path_ends_with_kmod_ext(teststr[i].val,
+                                                     strlen(teststr[i].val)) ==
+                                     teststr[i].res,
                              EXIT_FAILURE);
        }
 
index 66c4fce64e4230b79a380b1d5bed9a29dc792852..49b1a7b3621d5d91d82039657407950ec290d206 100644 (file)
        test_spawn_prog(TOOLS_DIR "/modprobe", \
                        (const char *[]){ TOOLS_DIR "/modprobe", ##__VA_ARGS__, NULL })
 
-static const char *const mod_name[] = {
-       "mod-loop-b",
-       "mod-weakdep",
-       NULL,
-};
-
 static int test_weakdep(void)
 {
+       static const char *const mod_name[] = {
+               "mod-loop-b",
+               "mod-weakdep",
+       };
        struct kmod_ctx *ctx;
-       int mod_name_index = 0;
        int err;
 
        ctx = kmod_new(NULL, NULL);
        if (ctx == NULL)
                exit(EXIT_FAILURE);
 
-       while (mod_name[mod_name_index]) {
+       for (size_t i = 0; i < ARRAY_SIZE(mod_name); i++) {
                struct kmod_list *list = NULL;
                struct kmod_module *mod = NULL;
                struct kmod_list *mod_list = NULL;
                struct kmod_list *itr = NULL;
 
-               printf("%s:", mod_name[mod_name_index]);
-               err = kmod_module_new_from_lookup(ctx, mod_name[mod_name_index], &list);
+               printf("%s:", mod_name[i]);
+               err = kmod_module_new_from_lookup(ctx, mod_name[i], &list);
                if (list == NULL || err < 0) {
                        fprintf(stderr, "module %s not found in directory %s\n",
-                               mod_name[mod_name_index],
-                               ctx ? kmod_get_dirname(ctx) : "(missing)");
+                               mod_name[i], ctx ? kmod_get_dirname(ctx) : "(missing)");
                        exit(EXIT_FAILURE);
                }
 
@@ -54,7 +50,7 @@ static int test_weakdep(void)
                err = kmod_module_get_weakdeps(mod, &mod_list);
                if (err) {
                        fprintf(stderr, "weak dependencies can not be read for %s (%d)\n",
-                               mod_name[mod_name_index], err);
+                               mod_name[i], err);
                        exit(EXIT_FAILURE);
                }
 
@@ -70,8 +66,6 @@ static int test_weakdep(void)
                kmod_module_unref_list(mod_list);
                kmod_module_unref(mod);
                kmod_module_unref_list(list);
-
-               mod_name_index++;
        }
 
        kmod_unref(ctx);