diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-21 21:39:15 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-21 21:39:15 +0200 |
commit | 0ba4f18ea6981b4d2b4eded11b2da4b2a2192d5b (patch) | |
tree | b10104c574b719cf78f10945526a21a9fd385d64 /idle.c | |
parent | 003159e92bb4526845a8a1a1a4627824e939cd4b (diff) |
Finish up the assert conversion
Diffstat (limited to 'idle.c')
-rw-r--r-- | idle.c | 39 |
1 files changed, 29 insertions, 10 deletions
@@ -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; |