From 003159e92bb4526845a8a1a1a4627824e939cd4b Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Sun, 21 Jun 2020 11:43:49 +0200 Subject: Convert three more files to use assert --- announce.c | 34 ++++++++++++++++++++-------------- cfgdir.c | 39 ++++++++++++++++++++++++++------------- config.c | 20 ++++++++++---------- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/announce.c b/announce.c index dd93bec..ba98fef 100644 --- a/announce.c +++ b/announce.c @@ -21,25 +21,22 @@ mcast_free(struct uring_task *task) { struct announce *aev = container_of(task, struct announce, mcast_task); + assert_return(task); debug(DBG_ANN, "task %p, aev %p", task, aev); } static void mcast_sent(struct cfg *cfg, struct uring_task *task, int res) { - struct server *server; + struct server *server = container_of(task->tbuf, struct server, mcast_buf); + + assert_return(task && task->tbuf); if (res < 0) error("failure %i", res); else debug(DBG_ANN, "result %i", res); - if (!task || !task->tbuf) { - error("task or task->tbuf not set"); - return; - } - - server = container_of(task->tbuf, struct server, mcast_buf); uring_task_put(cfg, &server->task); } @@ -48,7 +45,10 @@ mcast_send(struct cfg *cfg, struct announce *aev, struct server *server) { int len; - if (!server || !server->pretty_name || server->announce_port < 1) + assert_return(cfg && aev && server); + + /* FIXME: should these be assert:ed as well? */ + if (!server->pretty_name || server->announce_port < 1) return; len = snprintf(server->mcast_buf.buf, sizeof(server->mcast_buf.buf), @@ -71,6 +71,8 @@ mcast_send_all(struct cfg *cfg, struct announce *aev) { struct server *server; + assert_return(cfg && aev); + list_for_each_entry(server, &cfg->servers, list) { verbose("Announcing server: %s", server->name); mcast_send(cfg, aev, server); @@ -82,6 +84,7 @@ announce_cb(struct cfg *cfg, struct uring_task *task, int res) { struct announce *aev = container_of(task, struct announce, task); + assert_return(cfg && task); assert_task_alive(DBG_ANN, task); if (res != sizeof(aev->value)) { @@ -99,6 +102,7 @@ announce_free(struct uring_task *task) { struct announce *aev = container_of(task, struct announce, task); + assert_return(task); debug(DBG_ANN, "task %p, aev 0x%p", task, aev); xfree(aev); } @@ -106,8 +110,7 @@ announce_free(struct uring_task *task) void announce_refdump(struct announce *aev) { - if (!aev) - return; + assert_return(aev); uring_task_refdump(&aev->task); uring_task_refdump(&aev->mcast_task); @@ -116,10 +119,7 @@ announce_refdump(struct announce *aev) void announce_delete(struct cfg *cfg) { - if (!cfg->aev) { - error("missing parameters"); - return; - } + assert_return(cfg && cfg->aev); debug(DBG_ANN, "closing fd %i", cfg->aev->task.fd); uring_task_destroy(cfg, &cfg->aev->mcast_task); @@ -141,6 +141,8 @@ announce_stop(struct announce *aev) } }; + assert_return(aev); + if (timerfd_settime(aev->task.fd, 0, &tspec, NULL) != 0) error("timerfd_settime: %m"); } @@ -159,6 +161,8 @@ announce_start(struct announce *aev) } }; + assert_return(aev); + if (timerfd_settime(aev->task.fd, 0, &tspec, NULL) != 0) error("timerfd_settime: %m"); } @@ -170,6 +174,8 @@ announce_init(struct cfg *cfg) int afd; int sfd; + assert_return(cfg); + aev = zmalloc(sizeof(*aev)); if (!aev) die("malloc: %m"); diff --git a/cfgdir.c b/cfgdir.c index 923926b..54d6450 100644 --- a/cfgdir.c +++ b/cfgdir.c @@ -27,10 +27,7 @@ scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct cfg *, struct server struct addrinfo *results = NULL, *ai; int r; - if (!dns || !dns->priv || !server_cb) { - error("invalid arguments"); - return; - } + assert_return(dns && dns->priv && server_cb); server = dns->priv; cfg = server->cfg; @@ -93,18 +90,24 @@ out: static void scfg_local_dns_cb(struct dns_async *dns) { + assert_return(dns); + scfg_dns_cb(dns, server_add_local); } static void scfg_remote_dns_cb(struct dns_async *dns) { + assert_return(dns); + scfg_dns_cb(dns, server_add_remote); } static void scfg_rcon_dns_cb(struct dns_async *dns) { + assert_return(dns); + scfg_dns_cb(dns, server_add_rcon); } @@ -193,6 +196,8 @@ handle_dns(struct cfg *cfg, struct server *server, const char *type, struct saddr *saddr, *tmp; struct dns_async *dns; + assert_return(cfg && server && type && value && async_cb && sync_cb, false); + switch (value->type) { case CFG_VAL_TYPE_ADDRS: debug(DBG_DNS, "%s: got immediate addrs", type); @@ -222,7 +227,11 @@ handle_dns(struct cfg *cfg, struct server *server, const char *type, static void scfg_parse(struct cfg *cfg, struct server *server) { - char *pos = &server->tbuf.buf[0]; + char *pos; + + assert_return(cfg && server); + + pos = server->tbuf.buf; if (!config_parse_header(cfg, server->name, "server", &pos)) return; @@ -341,6 +350,7 @@ scfg_read_cb(struct cfg *cfg, struct uring_task *task, int res) { struct server *server = container_of(task, struct server, task); + assert_return(cfg && task); assert_task_alive(DBG_CFG, task); if (res <= 0) { @@ -360,6 +370,7 @@ scfg_open_cb(struct cfg *cfg, struct uring_task *task, int res) { struct server *server = container_of(task, struct server, task); + assert_return(cfg && task); assert_task_alive(DBG_CFG, task); if (res < 0) { @@ -401,11 +412,9 @@ inotify_free(struct uring_task *task) struct inotify_ev *iev = container_of(task, struct inotify_ev, task); struct cfg *cfg = container_of(task->parent, struct cfg, task); - debug(DBG_CFG, "called"); - - if (!iev || !cfg) - die("iev or cfg is NULL!?"); + assert_return(task && iev && cfg); + debug(DBG_CFG, "called"); xfree(iev); cfg->iev = NULL; } @@ -413,6 +422,8 @@ inotify_free(struct uring_task *task) static void inotify_event_dump(const struct inotify_event *event) { + assert_return(event); + debug(DBG_CFG, "inotify event:"); debug(DBG_CFG, " * WD : %i", event->wd); debug(DBG_CFG, " * Cookie : %" PRIu32, event->cookie); @@ -459,6 +470,7 @@ inotify_cb(struct cfg *cfg, struct uring_task *task, int res) char *ptr; struct server *server; + assert_return(cfg && task); assert_task_alive(DBG_CFG, task); if (res <= 0) { @@ -499,16 +511,15 @@ inotify_cb(struct cfg *cfg, struct uring_task *task, int res) void cfgdir_refdump(struct inotify_ev *iev) { + assert_return(iev); + uring_task_refdump(&iev->task); } void cfgdir_delete(struct cfg *cfg) { - if (!cfg->iev) { - error("missing arguments"); - return; - } + assert_return(cfg && cfg->iev); debug(DBG_CFG, "closing fd %i", cfg->iev->task.fd); uring_task_destroy(cfg, &cfg->iev->task); @@ -525,6 +536,8 @@ cfgdir_init(struct cfg *cfg) struct dirent *dent; struct server *server; + assert_return(cfg); + iev = zmalloc(sizeof(*iev)); if (!iev) die("malloc: %m"); diff --git a/config.c b/config.c index f511dbd..f49baad 100644 --- a/config.c +++ b/config.c @@ -16,6 +16,8 @@ static void eat_whitespace_and_comments(char **pos) { + assert_return(pos && *pos); + while (true) { while (isspace(**pos)) (*pos)++; @@ -31,9 +33,11 @@ eat_whitespace_and_comments(char **pos) static char * get_line(char **pos) { - char *begin = *pos; - char *end; + char *begin, *end; + + assert_return(pos && *pos, NULL); + begin = *pos; while (isspace(*begin)) begin++; @@ -70,8 +74,7 @@ dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, bool async) bool rv = false; int r; - if (port < 1 || strlen(name) >= sizeof(dns->name)) - goto out; + assert_return(!empty_str(name) && strlen(name) < sizeof(dns->name) && port > 0 && rvalue, false); if (async) { rvalue->type = CFG_VAL_TYPE_ASYNC_ADDRS; @@ -166,8 +169,7 @@ strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async) struct list_head *list; unsigned naddrs = 0; - if (!str || *str == '\0' || !rvalue) - return false; + assert_return(!empty_str(str) && rvalue, false); rvalue->type = CFG_VAL_TYPE_ADDRS; list = &rvalue->saddrs; @@ -320,8 +322,7 @@ config_parse_line(struct cfg *cfg, const char *filename, char **buf, char *line, *tmp, *key; int i; - if (!cfg || !buf || !*buf || !kvmap || !rkey || !rkeyname || !rvalue) - die("%s: invalid parameters", filename); + assert_return(cfg && buf && *buf && kvmap && rkey && rkeyname && rvalue, false); eat_whitespace_and_comments(buf); line = get_line(buf); @@ -470,8 +471,7 @@ config_parse_header(struct cfg *cfg, const char *filename, const char *title, { char *line; - if (!cfg || !title || !buf || !*buf) - return false; + assert_return(cfg && !empty_str(filename) && !empty_str(title) && buf && *buf, false); eat_whitespace_and_comments(buf); -- cgit v1.2.3