From e0f263831011a0fbfe3075f171e98287ac9dae62 Mon Sep 17 00:00:00 2001 From: Ismael Arias Date: Thu, 27 Aug 2026 21:56:30 +0200 Subject: [PATCH] replace error switches with static lookup tables --- lib/src/discovery.c | 24 +++++++++------------ lib/src/monitor.c | 45 +++++++++++++++------------------------ lib/src/ratelimit.c | 51 +++++++++++++++++---------------------------- 3 files changed, 46 insertions(+), 74 deletions(-) diff --git a/lib/src/discovery.c b/lib/src/discovery.c index c76aa99..225b56b 100644 --- a/lib/src/discovery.c +++ b/lib/src/discovery.c @@ -291,20 +291,16 @@ void destroy_process_list(process_list *list) { } const char *discovery_code_string(discovery_code code) { - switch (code) { - case DISCOVERY_OK: - return "Success"; - case DISCOVERY_ALLOC: - return "Failed to allocate memory"; - case DISCOVERY_SOCKET: - return "Failed to create netlink socket"; - case DISCOVERY_BIND: - return "Failed to bind netlink socket"; - case DISCOVERY_RECVMSG: - return "Failed to receive netlink messages"; - case DISCOVERY_NETLINK_MSG: - return "Netlink error received"; - default: + static const char *const strings[] = { + [DISCOVERY_OK] = "Success", + [DISCOVERY_ALLOC] = "Failed to allocate memory", + [DISCOVERY_SOCKET] = "Failed to create netlink socket", + [DISCOVERY_BIND] = "Failed to bind netlink socket", + [DISCOVERY_RECVMSG] = "Failed to receive netlink messages", + [DISCOVERY_NETLINK_MSG] = "Netlink error received", + }; + if ((unsigned)code >= sizeof(strings) / sizeof(strings[0])) { return "Unknown error"; } + return strings[code]; } diff --git a/lib/src/monitor.c b/lib/src/monitor.c index 08fdb3a..6788f9b 100644 --- a/lib/src/monitor.c +++ b/lib/src/monitor.c @@ -401,34 +401,23 @@ monitor_code monitor_stop(void) { } const char *monitor_code_string(monitor_code code) { - switch (code) { - case MONITOR_OK: - return "Success"; - case MONITOR_SOCKET: - return "Failed to create socket"; - case MONITOR_CONNECT: - return "Failed to connect to monitor daemon"; - case MONITOR_BIND: - return "Failed to bind socket"; - case MONITOR_LISTEN: - return "Failed to listen on socket"; - case MONITOR_PIPE: - return "Failed to create pipe"; - case MONITOR_FORK: - return "Failed to fork daemon process"; - case MONITOR_WRITE: - return "Failed to write to socket"; - case MONITOR_READ: - return "Failed to read from socket"; - case MONITOR_PIDFD: - return "Failed to create pidfd"; - case MONITOR_SLOTS: - return "No available slots for watching PIDs"; - case MONITOR_NOT_FOUND: - return "PID not found in watch list"; - case MONITOR_TIMEOUT: - return "Timeout waiting for daemon startup"; - default: + static const char *const strings[] = { + [MONITOR_OK] = "Success", + [MONITOR_SOCKET] = "Failed to create socket", + [MONITOR_CONNECT] = "Failed to connect to monitor daemon", + [MONITOR_BIND] = "Failed to bind socket", + [MONITOR_LISTEN] = "Failed to listen on socket", + [MONITOR_PIPE] = "Failed to create pipe", + [MONITOR_FORK] = "Failed to fork daemon process", + [MONITOR_WRITE] = "Failed to write to socket", + [MONITOR_READ] = "Failed to read from socket", + [MONITOR_PIDFD] = "Failed to create pidfd", + [MONITOR_SLOTS] = "No available slots for watching PIDs", + [MONITOR_NOT_FOUND] = "PID not found in watch list", + [MONITOR_TIMEOUT] = "Timeout waiting for daemon startup", + }; + if ((unsigned)code >= sizeof(strings) / sizeof(strings[0])) { return "Unknown error"; } + return strings[code]; } diff --git a/lib/src/ratelimit.c b/lib/src/ratelimit.c index 9acb781..3d1870d 100644 --- a/lib/src/ratelimit.c +++ b/lib/src/ratelimit.c @@ -483,38 +483,25 @@ int get_rate_limits_from_cgroup(pid_t pid, uint32_t *upload_kbps, uint32_t *down } const char *ratelimit_code_string(ratelimit_code code) { - switch (code) { - case RATELIMIT_OK: - return "Success"; - case RATELIMIT_INVALID_PID: - return "Invalid PID"; - case RATELIMIT_ALLOC: - return "Memory allocation failed"; - case RATELIMIT_OPEN_CGROUP: - return "Failed to open cgroup"; - case RATELIMIT_BPF_OPEN: - return "Failed to open BPF object"; - case RATELIMIT_BPF_LOAD: - return "Failed to load BPF program"; - case RATELIMIT_BPF_LINK: - return "Failed to create BPF link"; - case RATELIMIT_BPF_PIN: - return "Failed to pin BPF link"; - case RATELIMIT_CGROUP_NOT_FOUND: - return "No rate limit set for PID"; - case RATELIMIT_LIBCG_INIT: - return "Failed to initialize libcgroup"; - case RATELIMIT_LIBCG_CREATE: - return "Failed to create cgroup"; - case RATELIMIT_LIBCG_ATTACH: - return "Failed to attach process to cgroup"; - case RATELIMIT_LIBCG_DELETE: - return "Failed to delete cgroup"; - case RATELIMIT_NO_CGROUP2: - return "cgroup v2 is required but not available"; - case RATELIMIT_NO_BPFFS: - return "bpffs is required but not available"; - default: + static const char *const strings[] = { + [RATELIMIT_OK] = "Success", + [RATELIMIT_INVALID_PID] = "Invalid PID", + [RATELIMIT_ALLOC] = "Memory allocation failed", + [RATELIMIT_OPEN_CGROUP] = "Failed to open cgroup", + [RATELIMIT_BPF_OPEN] = "Failed to open BPF object", + [RATELIMIT_BPF_LOAD] = "Failed to load BPF program", + [RATELIMIT_BPF_LINK] = "Failed to create BPF link", + [RATELIMIT_BPF_PIN] = "Failed to pin BPF link", + [RATELIMIT_CGROUP_NOT_FOUND] = "No rate limit set for PID", + [RATELIMIT_LIBCG_INIT] = "Failed to initialize libcgroup", + [RATELIMIT_LIBCG_CREATE] = "Failed to create cgroup", + [RATELIMIT_LIBCG_ATTACH] = "Failed to attach process to cgroup", + [RATELIMIT_LIBCG_DELETE] = "Failed to delete cgroup", + [RATELIMIT_NO_CGROUP2] = "cgroup v2 is required but not available", + [RATELIMIT_NO_BPFFS] = "bpffs is required but not available", + }; + if ((unsigned)code >= sizeof(strings) / sizeof(strings[0])) { return "Unknown error"; } + return strings[code]; }