Skip to content

Commit ee993d3

Browse files
committed
module: module_class_name handle nfound
+ refactor a bit (early return if nfond, use ARR_COUNT) + make module_class_name_pairs const to make clang_tidy happy
1 parent d8a4561 commit ee993d3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/module.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "debug.h"
4343
#include "module.h"
4444
#include "utils/list.h"
45+
#include "utils/macros.h" // for ARR_COUNT, to_fourcc
4546

4647
#define MOD_NAME "[module] "
4748
#define MODULE_MAGIC to_fourcc('M', 'O', 'D', ' ')
@@ -156,7 +157,7 @@ void module_done(struct module *module_data)
156157
free(module_priv);
157158
}
158159

159-
static const char *module_class_name_pairs[] = {
160+
static const char *const module_class_name_pairs[] = {
160161
[MODULE_CLASS_ROOT] = "root",
161162
[MODULE_CLASS_PORT] = "port",
162163
[MODULE_CLASS_COMPRESS] = "compress",
@@ -176,10 +177,15 @@ static const char *module_class_name_pairs[] = {
176177

177178
const char *module_class_name(enum module_class cls)
178179
{
179-
if((unsigned int) cls < sizeof(module_class_name_pairs)/sizeof(const char *))
180-
return module_class_name_pairs[cls];
181-
else
180+
if ((unsigned int) cls >= ARR_COUNT(module_class_name_pairs)) {
181+
MSG(ERROR, "No name for module class %d!\n", (int) cls);
182182
return NULL;
183+
}
184+
const char *name = module_class_name_pairs[cls];
185+
if (name == NULL) { // eg. for MODULE_CLASS_NONE
186+
MSG(ERROR, "Undefined name for module class %d!\n", (int) cls);
187+
}
188+
return name;
183189
}
184190

185191
void

0 commit comments

Comments
 (0)