From 0ba4f18ea6981b4d2b4eded11b2da4b2a2192d5b Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Sun, 21 Jun 2020 21:39:15 +0200 Subject: Finish up the assert conversion --- announce.c | 8 ++-- cfgdir.c | 2 +- idle.c | 39 +++++++++++---- igmp.c | 77 +++++++++++++++++------------- main.c | 42 ++++++++++++---- main.h | 12 +++++ proxy.c | 27 +++++++++-- rcon.c | 67 +++++++++++++++++--------- server.c | 10 ++-- systemd.c | 23 ++++----- uring.c | 158 +++++++++++++++++++++++++++++-------------------------------- uring.h | 2 +- utils.c | 54 +++++++++++++++------ 13 files changed, 323 insertions(+), 198 deletions(-) diff --git a/announce.c b/announce.c index ba98fef..9195cd2 100644 --- a/announce.c +++ b/announce.c @@ -30,7 +30,7 @@ mcast_sent(struct cfg *cfg, struct uring_task *task, int res) { struct server *server = container_of(task->tbuf, struct server, mcast_buf); - assert_return(task && task->tbuf); + assert_return(cfg && task && task->tbuf); if (res < 0) error("failure %i", res); @@ -110,7 +110,7 @@ announce_free(struct uring_task *task) void announce_refdump(struct announce *aev) { - assert_return(aev); + assert_return_silent(aev); uring_task_refdump(&aev->task); uring_task_refdump(&aev->mcast_task); @@ -188,10 +188,10 @@ announce_init(struct cfg *cfg) if (sfd < 0) die("socket: %m"); - uring_task_init(&aev->task, "aev", uring_parent(cfg), announce_free); + uring_task_init(cfg, &aev->task, "aev", uring_parent(cfg), announce_free); uring_task_set_fd(&aev->task, afd); - uring_task_init(&aev->mcast_task, "aev_mcast", &aev->task, mcast_free); + uring_task_init(cfg, &aev->mcast_task, "aev_mcast", &aev->task, mcast_free); uring_task_set_fd(&aev->mcast_task, sfd); saddr_set_ipv4(&aev->mcast_task.saddr, cinet_addr(224,0,2,60), htons(4445)); diff --git a/cfgdir.c b/cfgdir.c index 54d6450..8f6d9a3 100644 --- a/cfgdir.c +++ b/cfgdir.c @@ -555,7 +555,7 @@ cfgdir_init(struct cfg *cfg) if (iwd < 0) die("inotify_add_watch: %m"); - uring_task_init(&iev->task, "iev", uring_parent(cfg), inotify_free); + uring_task_init(cfg, &iev->task, "iev", uring_parent(cfg), inotify_free); uring_task_set_fd(&iev->task, ifd); cfg->iev = iev; uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), inotify_cb); diff --git a/idle.c b/idle.c index 4bd0656..ef588fd 100644 --- a/idle.c +++ b/idle.c @@ -30,12 +30,15 @@ idle_check_free(struct uring_task *task) { struct idle *idle = container_of(task, struct idle, idlecheck); + assert_return(task && idle); debug(DBG_IDLE, "task %p, idle %p", task, idle); } static inline void write_byte(char **pos, char byte) { + assert_return(pos && *pos); + **pos = byte; (*pos)++; } @@ -49,6 +52,8 @@ write_byte(char **pos, char byte) static inline void write_varint(char **pos, int32_t orig) { + assert_return(pos && *pos); + uint32_t val = (uint32_t)orig; while (val) { @@ -72,6 +77,8 @@ read_varint(char **pos, size_t *remain, int32_t *res) unsigned consumed; uint32_t val = 0; + assert_return(pos && *pos && remain && res, -1); + for (consumed = 1; consumed <= *remain; consumed++) { uint32_t tmp; @@ -96,6 +103,8 @@ read_varint(char **pos, size_t *remain, int32_t *res) static inline void write_bytes(char **pos, const char *bytes, size_t n) { + assert_return(pos && *pos && bytes && n > 0); + memcpy(*pos, bytes, n); *pos += n; } @@ -103,12 +112,16 @@ write_bytes(char **pos, const char *bytes, size_t n) static inline void write_str(char **pos, const char *str) { + assert_return(pos && *pos && !empty_str(str)); + write_bytes(pos, str, strlen(str)); } static inline void write_cmd(char **pos, const char *begin, const char *end) { + assert_return(pos && *pos && begin && end && end > begin); + write_varint(pos, end - begin); write_bytes(pos, begin, end - begin); } @@ -121,6 +134,7 @@ idle_check_handshake_complete(struct cfg *cfg, struct uring_task *task, int res) int32_t mclen; int r; + assert_return(cfg && task, -EINVAL); assert_task_alive_or(DBG_IDLE, task, return -EINTR); remain = task->tbuf->len; @@ -163,6 +177,8 @@ get_player_count(struct cfg *cfg, const char *pos, size_t remain) char *end; unsigned count; + assert_return(cfg && pos && remain > 0, -1); + online = memmem(pos, remain, ONLINE_NEEDLE, strlen(ONLINE_NEEDLE)); if (!online) { error("could not find online count in JSON"); @@ -197,6 +213,7 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) int player_count; int r; + assert_return(cfg && task); assert_task_alive(DBG_IDLE, task); debug(DBG_IDLE, "res: %i", res); @@ -280,6 +297,7 @@ idle_check_handshake_sent(struct cfg *cfg, struct uring_task *task, int res) { struct idle *idle = container_of(task, struct idle, idlecheck); + assert_return(cfg && task); assert_task_alive(DBG_IDLE, task); debug(DBG_IDLE, "sent %i bytes", res); @@ -303,6 +321,7 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected uint16_t port; char hostname[INET6_ADDRSTRLEN]; + assert_return(cfg && conn); assert_task_alive(DBG_IDLE, &idle->idlecheck); if (!connected) { @@ -322,7 +341,6 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected pos = buf; write_byte(&pos, MC_HELO); write_varint(&pos, -1); /* Protocol version, -1 = undefined */ - write_varint(&pos, strlen(hostname)); write_str(&pos, hostname); write_byte(&pos, (port >> 8) & 0xff); write_byte(&pos, (port >> 0) & 0xff); @@ -344,6 +362,7 @@ idle_cb(struct cfg *cfg, struct uring_task *task, int res) { struct idle *idle = container_of(task, struct idle, task); + assert_return(cfg && task); assert_task_alive(DBG_IDLE, task); if (res != sizeof(idle->value)) { @@ -367,6 +386,7 @@ idle_free(struct uring_task *task) { struct idle *idle = container_of(task, struct idle, task); + assert_return(task); debug(DBG_IDLE, "task %p, idle %p", task, idle); xfree(idle); } @@ -374,8 +394,7 @@ idle_free(struct uring_task *task) void idle_refdump(struct idle *idle) { - if (!idle) - return; + assert_return_silent(idle); uring_task_refdump(&idle->task); uring_task_refdump(&idle->idlecheck); @@ -384,11 +403,12 @@ idle_refdump(struct idle *idle) void idle_delete(struct cfg *cfg, struct server *server) { - struct idle *idle = server->idle; + struct idle *idle; - if (!idle) - return; + assert_return(cfg && server); + assert_return_silent(server->idle); + idle = server->idle; debug(DBG_IDLE, "closing fd %i", idle->task.fd); uring_task_destroy(cfg, &idle->idlecheck); uring_task_destroy(cfg, &idle->task); @@ -412,8 +432,7 @@ idle_init(struct cfg *cfg, struct server *server) } }; - if (!server) - return; + assert_return(cfg && server); if (server->idle_timeout < 1) return; @@ -429,9 +448,9 @@ idle_init(struct cfg *cfg, struct server *server) if (timerfd_settime(ifd, 0, &tspec, NULL) != 0) die("timerfd_settime: %m"); - uring_task_init(&idle->task, "idle", &server->task, idle_free); + uring_task_init(cfg, &idle->task, "idle", &server->task, idle_free); uring_task_set_fd(&idle->task, ifd); - uring_task_init(&idle->idlecheck, "idlecheck", &idle->task, idle_check_free); + uring_task_init(cfg, &idle->idlecheck, "idlecheck", &idle->task, idle_check_free); uring_task_set_buf(&idle->idlecheck, &idle->tbuf); idle->server = server; server->idle = idle; diff --git a/igmp.c b/igmp.c index b809e4b..01a4dc4 100644 --- a/igmp.c +++ b/igmp.c @@ -105,63 +105,68 @@ from32to16(unsigned int x) } static unsigned int -do_csum(const unsigned char *buff, int len) +do_csum(const unsigned char *buf, int len) { int odd; unsigned int result = 0; - if (len <= 0) - goto out; - odd = 1 & (unsigned long) buff; + assert_return(buf && len > 0, 0); + + odd = 1 & (unsigned long)buf; if (odd) { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - result += (*buff << 8); + result += (*buf << 8); #else - result = *buff; + result = *buf; #endif len--; - buff++; + buf++; } + if (len >= 2) { - if (2 & (unsigned long) buff) { - result += *(unsigned short *) buff; + if (2 & (unsigned long)buf) { + result += *(unsigned short *)buf; len -= 2; - buff += 2; + buf += 2; } if (len >= 4) { - const unsigned char *end = buff + ((unsigned)len & ~3); + const unsigned char *end = buf + ((unsigned)len & ~3); unsigned int carry = 0; do { - unsigned int w = *(unsigned int *) buff; - buff += 4; + unsigned int w = *(unsigned int *)buf; + buf += 4; result += carry; result += w; carry = (w > result); - } while (buff < end); + } while (buf < end); result += carry; result = (result & 0xffff) + (result >> 16); } if (len & 2) { - result += *(unsigned short *) buff; - buff += 2; + result += *(unsigned short *)buf; + buf += 2; } } + if (len & 1) #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - result += *buff; + result += *buf; #else - result += (*buff << 8); + result += (*buf << 8); #endif + result = from32to16(result); if (odd) result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); -out: + return result; } static inline bool csum_valid(const char *buf, size_t len) { + assert_return(buf && len > 0, false); + return do_csum((unsigned const char *)buf, len) == 0xffff; } @@ -180,12 +185,18 @@ igmp_match() static void igmp_parse(struct cfg *cfg, struct igmp *igmp) { - char *buf = igmp->task.tbuf->buf; - size_t len = igmp->task.tbuf->len; - struct ipv4_hdr *hdr = (struct ipv4_hdr *)buf; + char *buf; + size_t len; + struct ipv4_hdr *hdr; size_t body_len; union igmp_msg *igmp_msg; + assert_return(cfg && igmp); + + buf = igmp->task.tbuf->buf; + len = igmp->task.tbuf->len; + hdr = (struct ipv4_hdr *)buf; + if (len <= IPV4_MIN_HDR_LEN) return; @@ -389,10 +400,10 @@ igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res) { struct igmp *igmp = container_of(task, struct igmp, task); - debug(DBG_IGMP, "task %p, igmp %p, res %i", task, igmp, res); - + assert_return(cfg && task); assert_task_alive(DBG_IGMP, task); + debug(DBG_IGMP, "task %p, igmp %p, res %i", task, igmp, res); if (res < 0) { error("res: %i", res); return; @@ -414,6 +425,7 @@ igmp_free(struct uring_task *task) { struct igmp *igmp = container_of(task, struct igmp, task); + assert_return(task); debug(DBG_IGMP, "task %p, igmp %p", task, igmp); xfree(igmp); } @@ -421,8 +433,7 @@ igmp_free(struct uring_task *task) void igmp_refdump(struct igmp *igmp) { - if (!igmp) - return; + assert_return_silent(igmp); uring_task_refdump(&igmp->task); } @@ -430,13 +441,11 @@ igmp_refdump(struct igmp *igmp) void igmp_delete(struct cfg *cfg) { - struct igmp *igmp = cfg->igmp; + assert_return(cfg); + assert_return_silent(cfg->igmp); - if (!igmp) - return; - - debug(DBG_IGMP, "closing fd %i", igmp->task.fd); - uring_task_destroy(cfg, &igmp->task); + debug(DBG_IGMP, "closing fd %i", cfg->igmp->task.fd); + uring_task_destroy(cfg, &cfg->igmp->task); cfg->igmp = NULL; } @@ -497,6 +506,8 @@ igmp_init(struct cfg *cfg) int sfd; int opt; + assert_return(cfg); + if (!cfg->do_igmp) { debug(DBG_IGMP, "igmp snooping disabled"); return; @@ -567,7 +578,7 @@ igmp_init(struct cfg *cfg) } debug(DBG_IGMP, "init successful, using fd %i", sfd); - uring_task_init(&igmp->task, "igmp", uring_parent(cfg), igmp_free); + uring_task_init(cfg, &igmp->task, "igmp", uring_parent(cfg), igmp_free); uring_task_set_fd(&igmp->task, sfd); uring_task_set_buf(&igmp->task, &igmp->tbuf); igmp->task.saddr.addrlen = sizeof(igmp->task.saddr.ll); diff --git a/main.c b/main.c index ba9ea37..5d99711 100644 --- a/main.c +++ b/main.c @@ -57,6 +57,8 @@ msg(enum debug_lvl lvl, const char *fmt, va_list ap) const char *color; const char *sd_lvl; + assert_return(lvl != 0 && !empty_str(fmt) && ap); + while (first) { int fd; const char *e; @@ -149,6 +151,8 @@ __debug(enum debug_lvl lvl, const char *fmt, ...) { va_list ap; + assert_return(lvl != 0 && !empty_str(fmt)); + va_start(ap, fmt); msg(lvl, fmt, ap); va_end(ap); @@ -159,9 +163,13 @@ __die(const char *fmt, ...) { va_list ap; - va_start(ap, fmt); - msg(DBG_ERROR, fmt, ap); - va_end(ap); + if (!empty_str(fmt)) { + va_start(ap, fmt); + msg(DBG_ERROR, fmt, ap); + va_end(ap); + } else + error("fmt not set"); + sd_notifyf(0, "STATUS=Error, shutting down"); exit(EXIT_FAILURE); }; @@ -171,6 +179,8 @@ cfg_free(struct uring_task *task) { struct cfg *cfg = container_of(task, struct cfg, task); + assert_return(task); + debug(DBG_SIG, "called"); systemd_delete(cfg); xfree(cfg->igmp_iface); @@ -213,6 +223,8 @@ cfg_read(struct cfg *cfg) size_t rd = 0; size_t r; + assert_return(cfg); + if (cfg->cfg_path) path = cfg->cfg_path; else @@ -353,7 +365,7 @@ usage(int argc, char **argv, bool invalid) " -d, --debug=CATEGORY\tenable debugging for CATEGORY\n" "\t\t\t(use \"list\" to see available categories,\n" "\t\t\t or \"all\" to enable all categories)\n", - argv[0]); + argv ? argv[0] : "mcproxy"); exit(invalid ? EXIT_FAILURE : EXIT_SUCCESS); } @@ -365,13 +377,15 @@ cfg_init(int argc, char **argv) int c; unsigned i; + assert_die(argc > 0 && argv, "invalid arguments"); + cfg = zmalloc(sizeof(*cfg)); if (!cfg) die("malloc: %m"); cfg->uid = geteuid(); cfg->gid = getegid(); - uring_task_init(&cfg->task, "cfg", NULL, cfg_free); + uring_task_init(cfg, &cfg->task, "cfg", NULL, cfg_free); list_init(&cfg->servers); while (true) { @@ -475,6 +489,9 @@ cfg_init(int argc, char **argv) static void cfg_apply(struct cfg *cfg) { + if (!cfg) + die("invalid arguments"); + if (cfg->uid == 0 || cfg->gid == 0) /* This catches both -u root and running as root without -u */ die("Execution as root is not supported (use -u )"); @@ -538,6 +555,8 @@ signalfd_free(struct uring_task *task) { struct signalfd_ev *sev = container_of(task, struct signalfd_ev, task); + assert_return(task); + debug(DBG_SIG, "called"); sev->cfg->sev = NULL; xfree(sev); @@ -548,6 +567,8 @@ dump_tree(struct cfg *cfg) { struct server *server; + assert_return(cfg); + if (!debug_enabled(DBG_REF)) return; @@ -576,6 +597,7 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) struct signalfd_ev *sev = container_of(task, struct signalfd_ev, task); struct server *server, *stmp; + assert_return(cfg && task); assert_task_alive(DBG_SIG, task); if (res != sizeof(sev->buf)) @@ -621,6 +643,8 @@ hack_handler(int signum, siginfo_t *info, void *ucontext) { uint64_t val; + assert_return(signum > 0 && info); + switch (signum) { case SIGUSR1: debug(DBG_SIG, "Got a SIGUSR1"); @@ -660,6 +684,8 @@ signalfd_init(struct cfg *cfg) //sigset_t mask; struct signalfd_ev *sev; + assert_return(cfg); + sev = zmalloc(sizeof(*sev)); if (!sev) die("malloc: %m"); @@ -667,11 +693,11 @@ signalfd_init(struct cfg *cfg) /* sigfillset(&mask); if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) - perrordie("sigprocmask"); + die("sigprocmask: %m"); sfd = signalfd(-1, &mask, SFD_CLOEXEC); if (sfd < 0) - perrordie("signalfd"); + die("signalfd: %m"); */ struct sigaction action; @@ -693,7 +719,7 @@ signalfd_init(struct cfg *cfg) die("eventfd: %m"); debug(DBG_SIG, "using fd %i", sfd); - uring_task_init(&sev->task, "sev", uring_parent(cfg), signalfd_free); + uring_task_init(cfg, &sev->task, "sev", uring_parent(cfg), signalfd_free); uring_task_set_fd(&sev->task, sfd); cfg->sev = sev; sev->cfg = cfg; diff --git a/main.h b/main.h index ccd7221..9759ecf 100644 --- a/main.h +++ b/main.h @@ -73,6 +73,18 @@ void __die(const char *fmt, ...) __attribute__((format(printf, 1, 2))); return __VA_ARGS__; \ } while (0) +#define assert_return_silent(expr, ...) \ + do { \ + if (!(expr)) \ + return __VA_ARGS__; \ + } while (0) + +#define assert_die(expr, msg) \ + do { \ + if (!assert_log(expr, #expr)) \ + die(msg); \ + } while (0) + #define assert_task_alive_or(lvl, t, cmd) \ do { \ if (!(t)) { \ diff --git a/proxy.c b/proxy.c index a629d7c..05117fd 100644 --- a/proxy.c +++ b/proxy.c @@ -10,6 +10,8 @@ void proxy_refdump(struct server_proxy *proxy) { + assert_return(proxy); + uring_task_refdump(&proxy->task); uring_task_refdump(&proxy->clienttask); uring_task_refdump(&proxy->servertask); @@ -21,6 +23,8 @@ format_bytes(char *buf, size_t len, uint64_t val) uint64_t tmp; const char *suffix = "B"; + assert_return(buf && len > 0); + tmp = val * 10; if (val > 1152921504606846976ULL) { tmp = val / 115292150460684697ULL; @@ -50,6 +54,8 @@ format_time(char *buf, size_t len, time_t diff) { unsigned hh, mm, ss; + assert_return(buf && len > 0); + hh = diff / 3600; diff %= 3600; mm = diff / 60; @@ -67,6 +73,8 @@ proxy_free(struct uring_task *task) char stc[100]; char duration[100]; + assert_return(task); + debug(DBG_PROXY, "server: %s, src: %s, dst: %s", proxy->server->name, proxy->client_conn.remote.addrstr, @@ -94,6 +102,8 @@ proxy_client_free(struct uring_task *task) { struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask); + assert_return(task); + debug(DBG_PROXY, "%s: client connection closed", proxy->server->name); } @@ -102,6 +112,8 @@ proxy_server_free(struct uring_task *task) { struct server_proxy *proxy = container_of(task, struct server_proxy, servertask); + assert_return(task); + debug(DBG_PROXY, "%s: server connection closed", proxy->server->name); } @@ -110,6 +122,8 @@ proxy_delete(struct cfg *cfg, struct server_proxy *proxy) { debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name, proxy); + assert_return(cfg && proxy); + uring_task_destroy(cfg, &proxy->servertask); uring_task_destroy(cfg, &proxy->clienttask); uring_task_destroy(cfg, &proxy->task); @@ -122,6 +136,7 @@ proxy_client_data_out(struct cfg *cfg, struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask); + assert_return(cfg && task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { @@ -141,6 +156,7 @@ proxy_client_data_in(struct cfg *cfg, struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask); + assert_return(cfg && task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { @@ -162,6 +178,7 @@ proxy_server_data_out(struct cfg *cfg, struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, servertask); + assert_return(cfg && task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { @@ -181,6 +198,7 @@ proxy_server_data_in(struct cfg *cfg, struct uring_task *task, int res) { struct server_proxy *proxy = container_of(task, struct server_proxy, servertask); + assert_return(cfg && task); assert_task_alive(DBG_PROXY, task); if (res <= 0) { @@ -199,6 +217,7 @@ proxy_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) { struct server_proxy *proxy = container_of(conn, struct server_proxy, server_conn); + assert_return(cfg && conn); assert_task_alive(DBG_PROXY, &proxy->clienttask); assert_task_alive(DBG_PROXY, &proxy->servertask); @@ -225,6 +244,8 @@ proxy_new(struct cfg *cfg, struct server *server, struct saddr *client, int fd) { struct server_proxy *proxy; + assert_return(cfg && server && client && fd > 0, NULL); + proxy = zmalloc(sizeof(*proxy)); if (!proxy) { error("malloc: %m"); @@ -234,17 +255,17 @@ proxy_new(struct cfg *cfg, struct server *server, struct saddr *client, int fd) proxy->sfd = -1; proxy->cfd = fd; proxy->server = server; - uring_task_init(&proxy->task, "proxy", &server->task, proxy_free); + uring_task_init(cfg, &proxy->task, "proxy", &server->task, proxy_free); connection_set_local(cfg, &proxy->client_conn, fd); connection_set_remote(cfg, &proxy->client_conn, client); - uring_task_init(&proxy->clienttask, "proxy_client", &proxy->task, + uring_task_init(cfg, &proxy->clienttask, "proxy_client", &proxy->task, proxy_client_free); uring_task_set_buf(&proxy->clienttask, &proxy->clientbuf); uring_task_set_fd(&proxy->clienttask, fd); - uring_task_init(&proxy->servertask, "proxy_server", &proxy->task, + uring_task_init(cfg, &proxy->servertask, "proxy_server", &proxy->task, proxy_server_free); uring_task_set_buf(&proxy->servertask, &proxy->serverbuf); diff --git a/rcon.c b/rcon.c index 1a57b89..dc29364 100644 --- a/rcon.c +++ b/rcon.c @@ -27,6 +27,8 @@ rcon_free(struct uring_task *task) { struct rcon *rcon = container_of(task, struct rcon, task); + assert_return(task); + debug(DBG_RCON, "task %p, idle %p", task, rcon); rcon->server->rcon = NULL; xfree(rcon); @@ -35,8 +37,7 @@ rcon_free(struct uring_task *task) void rcon_refdump(struct rcon *rcon) { - if (!rcon) - return; + assert_return_silent(rcon); uring_task_refdump(&rcon->task); } @@ -44,13 +45,10 @@ rcon_refdump(struct rcon *rcon) void rcon_delete(struct cfg *cfg, struct server *server) { - struct rcon *rcon = server->rcon; - - if (!rcon) - return; + assert_return_silent(server->rcon); - debug(DBG_RCON, "closing fd %i", rcon->task.fd); - uring_task_destroy(cfg, &rcon->task); + debug(DBG_RCON, "closing fd %i", server->rcon->task.fd); + uring_task_destroy(cfg, &server->rcon->task); server->rcon = NULL; } @@ -58,11 +56,14 @@ static int32_t read_int(char **pos, size_t *len) { uint32_t val; - char *p = *pos; + char *p; + + assert_return(pos && *pos, 0); if (len && *len < 4) return 0; + p = *pos; val = ((uint8_t)p[0] << 0); val += ((uint8_t)p[1] << 8); val += ((uint8_t)p[2] << 16); @@ -79,8 +80,11 @@ static void write_int(char **pos, size_t *len, int32_t orig) { uint32_t val = (uint32_t)orig; - char *p = *pos; + char *p; + + assert_return(pos && *pos); + p = *pos; p[0] = (val >> 0) & 0xff; p[1] = (val >> 8) & 0xff; p[2] = (val >> 16) & 0xff; @@ -94,8 +98,11 @@ write_int(char **pos, size_t *len, int32_t orig) static void write_str(char **pos, size_t *len, const char *str) { - size_t towrite = strlen(str); + size_t towrite; + assert_return(pos && *pos && !empty_str(str)); + + towrite = strlen(str); memcpy(*pos, str, towrite); *pos += towrite; if (len) @@ -105,8 +112,11 @@ write_str(char **pos, size_t *len, const char *str) static void write_end(char **pos, size_t *len) { - char *p = *pos; + char *p; + + assert_return(pos && *pos); + p = *pos; p[0] = 0x00; p[1] = 0x00; @@ -127,9 +137,12 @@ static void create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid, enum rcon_packet_type type, const char *msg) { - char *pos = &rcon->tbuf.buf[4]; + char *pos; + + assert_return(cfg && rcon && !empty_str(msg)); /* Body */ + pos = &rcon->tbuf.buf[4]; rcon->tbuf.len = 4; write_int(&pos, &rcon->tbuf.len, reqid); write_int(&pos, &rcon->tbuf.len, type); @@ -148,12 +161,15 @@ create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid, static int packet_complete(struct cfg *cfg, struct uring_task *task, int res) { - char *pos = task->tbuf->buf; - size_t len = task->tbuf->len; + char *pos; + size_t len; int32_t plen; + assert_return(cfg && task, 0); assert_task_alive_or(DBG_RCON, task, return -EINTR); + pos = task->tbuf->buf; + len = task->tbuf->len; if (task->tbuf->len < 14) return 0; @@ -171,10 +187,14 @@ static bool rcon_read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, int32_t *type, char **rmsg) { - char *pos = rcon->tbuf.buf; - size_t len = rcon->tbuf.len; + char *pos; + size_t len; int32_t plen; + assert_return(cfg && rcon && id && type && rmsg, false); + + pos = rcon->tbuf.buf; + len = rcon->tbuf.len; plen = read_int(&pos, &len); *id = read_int(&pos, &len); *type = read_int(&pos, &len); @@ -216,6 +236,7 @@ rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res) int32_t type; char *msg; + assert_return(cfg && task); assert_task_alive(DBG_RCON, task); if (res < 0) { @@ -245,6 +266,7 @@ rcon_stop_sent(struct cfg *cfg, struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); + assert_return(cfg && task); assert_task_alive(DBG_RCON, task); if (res < 0) { @@ -265,6 +287,7 @@ rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res) int32_t type; char *msg; + assert_return(cfg && task); assert_task_alive(DBG_RCON, task); if (res < 0) { @@ -300,6 +323,7 @@ rcon_login_sent(struct cfg *cfg, struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); + assert_return(cfg && task); assert_task_alive(DBG_RCON, task); if (res < 0) { @@ -317,6 +341,7 @@ rcon_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) { struct rcon *rcon = container_of(conn, struct rcon, conn); + assert_return(cfg && conn); assert_task_alive(DBG_RCON, &rcon->task); if (!connected) { @@ -335,17 +360,13 @@ rcon_init(struct cfg *cfg, struct server *server) { struct rcon *rcon; - if (!server) - return; - - if (list_empty(&server->rcons) || !server->rcon_password) - return; + assert_return(cfg && server && !list_empty(&server->rcons) && !empty_str(server->rcon_password)); rcon = zmalloc(sizeof(*rcon)); if (!rcon) die("malloc: %m"); - uring_task_init(&rcon->task, "rcon", &server->task, rcon_free); + uring_task_init(cfg, &rcon->task, "rcon", &server->task, rcon_free); uring_task_set_buf(&rcon->task, &rcon->tbuf); rcon->server = server; server->rcon = rcon; diff --git a/server.c b/server.c index c8f75f7..452e45f 100644 --- a/server.c +++ b/server.c @@ -58,8 +58,8 @@ server_refdump(struct server *server) uring_task_refdump(&local->task); list_for_each_entry(proxy, &server->proxys, list) proxy_refdump(proxy); - if (server->idle) - idle_refdump(server->idle); + idle_refdump(server->idle); + rcon_refdump(server->rcon); } static void @@ -642,7 +642,7 @@ server_add_local(struct cfg *cfg, struct server *server, struct saddr *saddr) debug(DBG_SRV, "adding local: %s", saddr->addrstr); local->local = *saddr; - uring_task_init(&local->task, "local", &server->task, server_local_free); + uring_task_init(cfg, &local->task, "local", &server->task, server_local_free); list_add(&local->list, &server->locals); xfree(saddr); return true; @@ -804,9 +804,9 @@ server_new(struct cfg *cfg, const char *name) server->name = xstrdup(name); server->stop_method = SERVER_STOP_METHOD_UNDEFINED; server->start_method = SERVER_START_METHOD_UNDEFINED; - uring_task_init(&server->task, "server", uring_parent(cfg), server_free); + uring_task_init(cfg, &server->task, "server", uring_parent(cfg), server_free); uring_task_set_buf(&server->task, &server->tbuf); - uring_task_init(&server->exec_task, "exec", &server->task, server_exec_free); + uring_task_init(cfg, &server->exec_task, "exec", &server->task, server_exec_free); list_init(&server->remotes); list_init(&server->locals); list_init(&server->proxys); diff --git a/systemd.c b/systemd.c index 1ddbd18..8752b4e 100644 --- a/systemd.c +++ b/systemd.c @@ -36,10 +36,7 @@ systemd_service_object_path(struct cfg *cfg, const char *service) char *d; const char *s; - if (empty_str(service)) { - error("invalid arguments"); - return NULL; - } + assert_return(cfg && service && !empty_str(service), NULL); r = zmalloc(strlen(SYSTEMD_DBUS_PATH_PREFIX) + strlen(service) * 3 + 1); if (!r) @@ -68,8 +65,8 @@ systemd_service_object_path(struct cfg *cfg, const char *service) void systemd_delete(struct cfg *cfg) { - if (!cfg->sd_bus) - return; + assert_return(cfg); + assert_return_silent(cfg->sd_bus); sd_bus_unref(cfg->sd_bus); cfg->sd_bus = NULL; @@ -80,6 +77,8 @@ get_bus(struct cfg *cfg) { int r; + assert_return(cfg, NULL); + if (cfg->sd_bus_failed) return NULL; @@ -112,11 +111,10 @@ systemd_service_running(struct cfg *cfg, struct server *server) sd_bus *bus = get_bus(cfg); sd_bus_error error = SD_BUS_ERROR_NULL; char *status = NULL; - int r; bool running = false; + int r; - if (!bus || !server->systemd_service || !server->systemd_obj) - return false; + assert_return(cfg && server && bus && server->systemd_service && server->systemd_obj, false); r = sd_bus_get_property_string(bus, SYSTEMD_DBUS_SERVICE, @@ -155,8 +153,7 @@ systemd_service_action(struct cfg *cfg, struct server *server, const char *actio bool performed = false; int r; - if (!bus || !server->systemd_service || !server->systemd_obj || !action) - return false; + assert_return(cfg && server && bus && server->systemd_service && server->systemd_obj && action, false); r = sd_bus_call_method(bus, SYSTEMD_DBUS_SERVICE, @@ -201,6 +198,8 @@ out: bool systemd_service_stop(struct cfg *cfg, struct server *server) { + assert_return(cfg && server, false); + return systemd_service_action(cfg, server, "Stop"); } @@ -216,6 +215,8 @@ systemd_service_stop(struct cfg *cfg, struct server *server) bool systemd_service_start(struct cfg *cfg, struct server *server) { + assert_return(cfg && server, false); + return systemd_service_action(cfg, server, "Start"); } diff --git a/uring.c b/uring.c index c38c028..f08ba9e 100644 --- a/uring.c +++ b/uring.c @@ -32,8 +32,7 @@ get_sqe(struct cfg *cfg, struct uring_task *task) { struct io_uring_sqe *sqe; - if (!cfg || !task) - die("invalid parameters"); + assert_die(cfg && task, "invalid arguments"); sqe = io_uring_get_sqe(&cfg->uev->uring); if (!sqe) { @@ -54,6 +53,8 @@ uring_task_refdump(struct uring_task *task) char buf[4096]; struct uring_task *tmp; + assert_return(task); + buf[0] = '\0'; for (tmp = task; tmp; tmp = tmp->parent) { size_t prefix; @@ -87,14 +88,11 @@ uring_task_refdump(struct uring_task *task) void uring_task_destroy(struct cfg *cfg, struct uring_task *task) { + assert_return(cfg && task); + debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - if (!task) { - error("called with no task"); - return; - } - if (task->fd >= 0) { struct io_uring_sqe *sqe; @@ -110,7 +108,9 @@ uring_task_destroy(struct cfg *cfg, struct uring_task *task) void uring_task_put(struct cfg *cfg, struct uring_task *task) { - struct uring_task *parent = task->parent; + struct uring_task *parent; + + assert_return(cfg && task); debug(DBG_REF, "task %s (%p), refcount %u", task->name, task, task->refcount); @@ -129,20 +129,21 @@ uring_task_put(struct cfg *cfg, struct uring_task *task) return; } - if (parent) - debug(DBG_REF, "putting parent %s (%p)", - task->parent->name, task->parent); - + parent = task->parent; if (task->free) task->free(task); - if (parent) + if (parent) { + debug(DBG_REF, "putting parent %s (%p)", parent->name, parent); uring_task_put(cfg, parent); + } } void uring_task_get(struct cfg *cfg, struct uring_task *task) { + assert_return(cfg && task); + debug(DBG_REF, "task %s (%p), refcount %u", task->name, task, task->refcount); @@ -155,25 +156,27 @@ uring_task_get(struct cfg *cfg, struct uring_task *task) void uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf) { + assert_return(task && tbuf); + debug(DBG_UR, "task %s (%p), buf %p, refcount %u", task->name, task, tbuf, task->refcount); - if (tbuf) { - /* iov_len and msg_namelen are set at send/receive time */ - tbuf->iov.iov_base = tbuf->buf; - tbuf->msg.msg_name = &task->saddr.storage; - tbuf->msg.msg_iov = &tbuf->iov; - tbuf->msg.msg_iovlen = 1; - tbuf->msg.msg_control = NULL; - tbuf->msg.msg_controllen = 0; - tbuf->msg.msg_flags = 0; - } + /* iov_len and msg_namelen are set at send/receive time */ + tbuf->iov.iov_base = tbuf->buf; + tbuf->msg.msg_name = &task->saddr.storage; + tbuf->msg.msg_iov = &tbuf->iov; + tbuf->msg.msg_iovlen = 1; + tbuf->msg.msg_control = NULL; + tbuf->msg.msg_controllen = 0; + tbuf->msg.msg_flags = 0; task->tbuf = tbuf; } void uring_task_set_fd(struct uring_task *task, int fd) { + assert_return(task); + debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, fd, task->refcount); @@ -183,6 +186,8 @@ uring_task_set_fd(struct uring_task *task, int fd) void uring_task_close_fd(struct cfg *cfg, struct uring_task *task) { + assert_return(cfg && task); + debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -196,28 +201,23 @@ uring_task_close_fd(struct cfg *cfg, struct uring_task *task) struct uring_task * uring_parent(struct cfg *cfg) { - if (!cfg) - die("called with null cfg"); - - if (!cfg->uev) - die("called with uninitialized uring"); + assert_die(cfg && cfg->uev, "invalid arguments"); return &cfg->uev->task; } void -uring_task_init(struct uring_task *task, const char *name, +uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, struct uring_task *parent, void (*free)(struct uring_task *)) { static bool first = true; + assert_die(cfg && task && !empty_str(name) && free, "invalid arguments"); + if (first) first = false; - else if (!parent) - die("called without a parent"); - - if (!free) - die("called without destructor"); + else + assert_die(parent, "called without a parent task"); task->refcount = 1; task->fd = -1; @@ -232,7 +232,7 @@ uring_task_init(struct uring_task *task, const char *name, "getting parent %s (%p), refcount %u", task->name, task, task->refcount, task->parent->name, task->parent, task->parent->refcount); - uring_task_get(NULL, task->parent); + uring_task_get(cfg, task->parent); } } @@ -241,14 +241,11 @@ uring_close(struct cfg *cfg, struct uring_task *task, int fd) { struct io_uring_sqe *sqe; + assert_return(cfg && task && fd >= 0); + debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - if (!task || fd < 0) { - error("invalid parameters (task: %p (%s), fd: %i)", task, task->name, fd); - return; - } - sqe = get_sqe(cfg, task); io_uring_prep_close(sqe, fd); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CLOSE)); @@ -259,8 +256,7 @@ uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) { int r; - if (!task || !task->tbuf || !task->final_cb) - die("missing parameters"); + assert_return(cfg && task && task->tbuf && task->final_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -289,10 +285,7 @@ finished: void uring_tbuf_write(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) { - if (!task || task->fd < 0 || !task->tbuf || task->tbuf->len < 0) { - error("invalid parameters"); - return; - } + assert_return(cfg && task && task->fd >= 0 && task->tbuf && task->tbuf->len > 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -307,10 +300,7 @@ uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, uta { struct io_uring_sqe *sqe; - if (task->fd < 0) { - error("no fd set"); - return; - } + assert_return(cfg && task && buf && len > 0 && cb && task->fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -326,10 +316,7 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) { int r; - if (!task || !task->tbuf || !task->final_cb || !task->is_complete_cb) { - error("invalid parameters"); - return; - } + assert_return(cfg && task && task->tbuf && task->final_cb && task->is_complete_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -368,10 +355,7 @@ void uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, rutask_cb_t is_complete_cb, utask_cb_t final_cb) { - if (!task || task->fd < 0 || !task->tbuf || !is_complete_cb || !final_cb) { - error("%s: invalid parameters", __func__); - return; - } + assert_return(cfg && task && task->fd >= 0 && task->tbuf && is_complete_cb && final_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -386,6 +370,7 @@ uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, static int uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res) { + assert_return(cfg && task && task->tbuf, -EINVAL); assert_task_alive_or(DBG_UR, task, return -EINTR); if (task->tbuf->len + 1 >= sizeof(task->tbuf->buf)) @@ -403,12 +388,16 @@ void uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) { + assert_return(cfg && task && task->tbuf && final_cb); + uring_tbuf_read_until(cfg, task, uring_tbuf_eof, final_cb); } static int uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) { + assert_return(cfg && task, -EINVAL); + if (res < 0) return res; else @@ -418,6 +407,8 @@ uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) void uring_tbuf_read(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) { + assert_return(cfg && task && final_cb); + uring_tbuf_read_until(cfg, task, uring_tbuf_have_data, final_cb); } @@ -426,10 +417,7 @@ uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t le { struct io_uring_sqe *sqe; - if (task->fd < 0) { - error("uring_read called with no fd set"); - return; - } + assert_return(cfg && task && buf && len > 0 && task->fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -445,6 +433,8 @@ uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, utask_c { struct io_uring_sqe *sqe; + assert_return(cfg && task && !empty_str(path) && cb); + debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -459,10 +449,7 @@ uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; - if (!task->tbuf) { - error("called with no tbuf set"); - return; - } + assert_return(cfg && task && task->fd >= 0 && task->tbuf && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -482,10 +469,7 @@ uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; - if (!task->tbuf) { - error("%s: called with no tbuf set", __func__); - return; - } + assert_return(cfg && task && task->fd >= 0 && task->tbuf && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -504,10 +488,7 @@ uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, uta { struct io_uring_sqe *sqe; - if (task->fd < 0) { - error("no fd set"); - return; - } + assert_return(cfg && task && task->fd >= 0 && saddr && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -523,10 +504,7 @@ uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utas { struct io_uring_sqe *sqe; - if (task->fd < 0) { - error("no fd set"); - return; - } + assert_return(cfg && task && task->fd >= 0 && saddr && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -543,10 +521,7 @@ uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, utask_cb_t { struct io_uring_sqe *sqe; - if (task->fd < 0) { - error("uring_poll called with no fd set"); - return; - } + assert_return(cfg && task && task->fd >= 0 && poll_mask && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -562,6 +537,8 @@ uring_poll_cancel(struct cfg *cfg, struct uring_task *task) { struct io_uring_sqe *sqe; + assert_return(cfg && task); + if (task->fd < 0) { /* not an error, no need to print error msg */ return; @@ -581,6 +558,8 @@ uring_free(struct uring_task *task) { struct uring_ev *uev = container_of(task, struct uring_ev, task); + assert_return(task); + debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -592,14 +571,19 @@ uring_free(struct uring_task *task) void uring_refdump(struct uring_ev *uev) { + assert_return(uev); + uring_task_refdump(&uev->task); } void uring_delete(struct cfg *cfg) { - struct uring_task *task = &cfg->uev->task; + struct uring_task *task; + + assert_return(cfg && cfg->uev); + task = &cfg->uev->task; debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -611,6 +595,8 @@ uring_init(struct cfg *cfg) { struct uring_ev *uev; + assert_return(cfg); + uev = zmalloc(sizeof(*uev)); if (!uev) die("malloc: %m"); @@ -621,7 +607,7 @@ uring_init(struct cfg *cfg) debug(DBG_UR, "uring initialized, features: 0x%08x", uev->uring_params.features); - uring_task_init(&uev->task, "uev", &cfg->task, uring_free); + uring_task_init(cfg, &uev->task, "uev", &cfg->task, uring_free); cfg->uev = uev; uev->cfg = cfg; } @@ -630,6 +616,8 @@ static inline void uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, struct io_uring_cqe *cqe) { + assert_return(cfg && !empty_str(type) && task && cqe); + debug(DBG_UR, "got CQE " "(type: %s, res: %i (%s), task: %s (%p), fd: %i, cb: %p)", type, @@ -644,6 +632,8 @@ uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, void uring_event_loop(struct cfg *cfg) { + assert_return(cfg); + while (true) { struct io_uring_cqe *cqe; unsigned nr, head; diff --git a/uring.h b/uring.h index 35cdec4..dbf7faf 100644 --- a/uring.h +++ b/uring.h @@ -20,7 +20,7 @@ void uring_task_close_fd(struct cfg *cfg, struct uring_task *task); struct uring_task *uring_parent(struct cfg *cfg); -void uring_task_init(struct uring_task *task, const char *name, +void uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, struct uring_task *parent, void (*free)(struct uring_task *)); diff --git a/utils.c b/utils.c index fc1b81a..ff18fb1 100644 --- a/utils.c +++ b/utils.c @@ -34,11 +34,13 @@ struct allocation { static void add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, size_t size) { - struct allocation *a = malloc(sizeof(*a)); + struct allocation *a; - debug(DBG_MALLOC, "called from %s:%i - %s(%zu) = %p", - callerfn, line, allocfn, size, ptr); + assert_die(!empty_str(allocfn) && !empty_str(callerfn) && line > 0 && ptr && size > 0, "invalid arguments"); + a = malloc(sizeof(*a)); + if (!a) + die("malloc: %m"); a->allocfn = allocfn; a->callerfn = callerfn; a->line = line; @@ -47,6 +49,8 @@ add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, s list_add(&a->list, &malloc_list); total_malloc_count++; malloc_count++; + debug(DBG_MALLOC, "called from %s:%i - %s(%zu) = %p (%p)", + callerfn, line, allocfn, size, ptr, a); } void * @@ -54,6 +58,8 @@ __zmalloc(const char *fn, int line, size_t size) { void *ptr; + assert_die(!empty_str(fn) && line > 0 && size > 0, "invalid arguments"); + ptr = calloc(1, size); if (ptr) add_allocation("zmalloc", fn, line, ptr, size); @@ -65,6 +71,8 @@ __xstrdup(const char *fn, int line, const char *s) { char *ptr; + assert_die(!empty_str(fn) && line > 0 && !empty_str(s), "invalid arguments"); + ptr = strdup(s); if (ptr) add_allocation("xstrdup", fn, line, ptr, strlen(s) + 1); @@ -76,6 +84,8 @@ __xstrndup(const char *fn, int line, const char *s, size_t n) { char *ptr; + assert_die(!empty_str(fn) && line > 0 && !empty_str(s) && n > 0, "invalid arguments"); + ptr = strndup(s, n); if (ptr) add_allocation("xstrndup", fn, line, ptr, n); @@ -88,6 +98,8 @@ __xfree(const char *fn, int line, void *ptr) struct allocation *a, *tmp; unsigned delete_count = 0; + assert_die(!empty_str(fn) && line > 0, "invalid arguments"); + if (!ptr) return; free(ptr); @@ -161,8 +173,7 @@ socket_set_low_latency(struct cfg *cfg, int sfd) { int option; - if (sfd <= 0) - return; + assert_return(cfg && sfd >= 0); /* FIXME: could make this configurable */ option = true; @@ -183,10 +194,11 @@ socket_set_low_latency(struct cfg *cfg, int sfd) void connection_set_local(struct cfg *cfg, struct connection *conn, int fd) { + assert_return(cfg && conn && fd >= 0); + conn->local.addrlen = sizeof(conn->local.storage); - if (fd < 0 || getsockname(fd, - (struct sockaddr *)&conn->local.storage, - &conn->local.addrlen) < 0) + if (getsockname(fd, (struct sockaddr *)&conn->local.storage, + &conn->local.addrlen) < 0) sprintf(conn->local.addrstr, ""); else saddr_set_addrstr(&conn->local); @@ -195,6 +207,8 @@ connection_set_local(struct cfg *cfg, struct connection *conn, int fd) void connection_set_remote(struct cfg *cfg, struct connection *conn, struct saddr *remote) { + assert_return(cfg && conn && remote); + conn->remote = *remote; saddr_set_addrstr(&conn->remote); } @@ -204,8 +218,11 @@ static void connect_next(struct cfg *cfg, struct uring_task *task, struct connec static void connect_cb(struct cfg *cfg, struct uring_task *task, int res) { - struct connection *conn = task->priv; + struct connection *conn; + + assert_return(cfg && task && task->priv); + conn = task->priv; if (res < 0) { debug(DBG_UR, "%s: connection to %s failed", task->name, conn->remote.addrstr); @@ -229,6 +246,7 @@ connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn) int sfd; unsigned i; + assert_return(cfg && task && conn && conn->callback); again: assert_task_alive_or(DBG_UR, task, goto out); @@ -275,10 +293,7 @@ connect_any(struct cfg *cfg, struct uring_task *task, struct list_head *addrs, struct connection *conn, void (*callback)(struct cfg *, struct connection *, bool res)) { - if (!cfg || !task || !addrs || !conn || !callback) { - error("invalid arguments"); - return; - } + assert_return(cfg && task && addrs && conn && callback); conn->next_addr = 0; conn->addrs = addrs; @@ -289,6 +304,8 @@ connect_any(struct cfg *cfg, struct uring_task *task, uint16_t saddr_port(struct saddr *saddr) { + assert_return(saddr, 0); + switch (saddr->storage.ss_family) { case AF_INET: return ntohs(saddr->in4.sin_port); @@ -302,6 +319,8 @@ saddr_port(struct saddr *saddr) char * saddr_addr(struct saddr *saddr, char *buf, size_t len) { + assert_return(saddr && buf && len > 0, NULL); + switch (saddr->storage.ss_family) { case AF_INET: if (inet_ntop(saddr->in4.sin_family, &saddr->in4.sin_addr, buf, len)) @@ -322,6 +341,8 @@ saddr_addr(struct saddr *saddr, char *buf, size_t len) void saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port) { + assert_return(saddr); + memset(&saddr->in4, 0, sizeof(saddr->in4)); saddr->in4.sin_family = AF_INET; saddr->in4.sin_port = port; @@ -333,6 +354,8 @@ saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port) void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port) { + assert_return(saddr && ip); + memset(&saddr->in6, 0, sizeof(saddr->in6)); saddr->in6.sin6_family = AF_INET6; saddr->in6.sin6_port = port; @@ -345,6 +368,8 @@ saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port) void saddr_set_addrstr(struct saddr *saddr) { + assert_return(saddr); + char abuf[ADDRSTRLEN]; switch (saddr->storage.ss_family) { @@ -372,8 +397,7 @@ strtou16_strict(const char *str, uint16_t *result) char *end; long val; - if (!str) - return -EINVAL; + assert_return(!empty_str(str) && result, -EINVAL); errno = 0; val = strtol(str, &end, 10); -- cgit v1.2.3