Make parser_misc keyword_table and rlimit_table unordered_maps

Besides of transitioning towards C++ this also eliminates the linear scan search that the functions using these arrays did.

Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
This commit is contained in:
Ryan Lee 2024-10-07 17:19:35 -07:00
parent 5b98577a4d
commit f6f3279c10

View file

@ -35,6 +35,7 @@
#include <sys/apparmor_private.h> #include <sys/apparmor_private.h>
#include <algorithm> #include <algorithm>
#include <unordered_map>
#include "capability.h" #include "capability.h"
#include "lib.h" #include "lib.h"
@ -61,6 +62,10 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size)
} }
#endif #endif
#ifndef NULL
#define NULL nullptr
#endif
int is_blacklisted(const char *name, const char *path) int is_blacklisted(const char *name, const char *path)
{ {
int retval = _aa_is_blacklisted(name); int retval = _aa_is_blacklisted(name);
@ -71,12 +76,7 @@ int is_blacklisted(const char *name, const char *path)
return !retval ? 0 : 1; return !retval ? 0 : 1;
} }
struct keyword_table { static const unordered_map<string, int> keyword_table = {
const char *keyword;
unsigned int token;
};
static struct keyword_table keyword_table[] = {
/* network */ /* network */
{"network", TOK_NETWORK}, {"network", TOK_NETWORK},
{"unix", TOK_UNIX}, {"unix", TOK_UNIX},
@ -132,11 +132,9 @@ static struct keyword_table keyword_table[] = {
{"sqpoll", TOK_SQPOLL}, {"sqpoll", TOK_SQPOLL},
{"all", TOK_ALL}, {"all", TOK_ALL},
{"priority", TOK_PRIORITY}, {"priority", TOK_PRIORITY},
/* terminate */
{NULL, 0}
}; };
static struct keyword_table rlimit_table[] = { static const unordered_map<string, int> rlimit_table = {
{"cpu", RLIMIT_CPU}, {"cpu", RLIMIT_CPU},
{"fsize", RLIMIT_FSIZE}, {"fsize", RLIMIT_FSIZE},
{"data", RLIMIT_DATA}, {"data", RLIMIT_DATA},
@ -162,37 +160,33 @@ static struct keyword_table rlimit_table[] = {
#ifdef RLIMIT_RTTIME #ifdef RLIMIT_RTTIME
{"rttime", RLIMIT_RTTIME}, {"rttime", RLIMIT_RTTIME},
#endif #endif
/* terminate */
{NULL, 0}
}; };
/* for alpha matches, check for keywords */ /* for alpha matches, check for keywords */
static int get_table_token(const char *name unused, struct keyword_table *table, static int get_table_token(const char *name unused, const unordered_map<string, int> &table,
const char *keyword) const string &keyword)
{ {
int i; auto token_entry = table.find(keyword);
if (token_entry == table.end()) {
for (i = 0; table[i].keyword; i++) { PDEBUG("Unable to find %s %s\n", name, keyword);
PDEBUG("Checking %s %s\n", name, table[i].keyword); return -1;
if (strcmp(keyword, table[i].keyword) == 0) { } else {
PDEBUG("Found %s %s\n", name, table[i].keyword); PDEBUG("Found %s %s\n", name, keyword.c_str());
return table[i].token; return token_entry->second;
}
} }
PDEBUG("Unable to find %s %s\n", name, keyword);
return -1;
} }
/* for alpha matches, check for keywords */ /* for alpha matches, check for keywords */
int get_keyword_token(const char *keyword) int get_keyword_token(const char *keyword)
{ {
return get_table_token("keyword", keyword_table, keyword); // Can't use string_view because that requires C++17
return get_table_token("keyword", keyword_table, string(keyword));
} }
int get_rlimit(const char *name) int get_rlimit(const char *name)
{ {
return get_table_token("rlimit", rlimit_table, name); // Can't use string_view because that requires C++17
return get_table_token("rlimit", rlimit_table, string(name));
} }