summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-21 21:39:15 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-21 21:39:15 +0200
commit0ba4f18ea6981b4d2b4eded11b2da4b2a2192d5b (patch)
treeb10104c574b719cf78f10945526a21a9fd385d64 /utils.c
parent003159e92bb4526845a8a1a1a4627824e939cd4b (diff)
Finish up the assert conversion
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c54
1 files changed, 39 insertions, 15 deletions
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, "<unknown>");
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);