testsuite: use ARRAY_SIZE() for iteration

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>
This commit is contained in:
Emil Velikov 2025-06-13 19:48:18 +01:00 committed by Lucas De Marchi
parent db92629b4f
commit 5cecea47dc
4 changed files with 29 additions and 40 deletions

View 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);

View 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;

View File

@ -19,26 +19,24 @@
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);
}

View File

@ -18,34 +18,30 @@
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);