commit c08b05552a6bb606d56c1e78f281ff49edc00ce2
parent b627d825995b92ace3b378c86f3513ddb1bb0764
Author: hhvn <dev@hhvn.uk>
Date: Sun, 8 May 2022 13:00:52 +0100
Report errors on failed sanity checks using assert_warn()
Diffstat:
11 files changed, 69 insertions(+), 100 deletions(-)
diff --git a/doc/DEBUG b/doc/DEBUG
@@ -0,0 +1,4 @@
+Include the following in config.mk:
+ DEBUG = -g3 -O0 -Wall -DASSERT_ALWAYS_DIES -DDIE_CORE
+
+Then make sure to `make clean`, before trying to debug something.
diff --git a/src/chan.c b/src/chan.c
@@ -88,11 +88,10 @@ struct Channel *
chan_add(struct Server *server, struct Channel **head, char *name, int query) {
struct Channel *channel, *p;
- if (!name)
- return NULL;
+ assert_warn(name, NULL);
- if ((channel = chan_create(server, name, query)) == NULL)
- return NULL;
+ channel = chan_create(server, name, query);
+ assert_warn(channel, NULL);
if (!*head) {
*head = channel;
@@ -114,7 +113,8 @@ chan_get(struct Channel **head, char *name, int old) {
/* if old is negative, match regardless of p->old
* else return only when p->old and old match */
- if (!head || !*head || !name)
+ assert_warn(head && name, NULL);
+ if (!*head)
return NULL;
for (p = *head; p; p = p->next) {
@@ -142,8 +142,7 @@ int
chan_remove(struct Channel **head, char *name) {
struct Channel *p;
- if (!head || !name)
- return -1;
+ assert_warn(head && name, -1);
if ((p = chan_get(head, name, -1)) == NULL)
return 0;
diff --git a/src/config.c b/src/config.c
@@ -60,8 +60,7 @@ struct Config *
config_getp(char *name) {
int i;
- if (!name)
- return NULL;
+ assert_warn(name, NULL);
for (i = 0; config[i].name; i++)
if (strcmp(config[i].name, name) == 0)
@@ -124,8 +123,7 @@ config_getr(char *name, long *a, long *b) {
void
config_setl(struct Config *conf, long num) {
- if (!conf)
- return;
+ assert_warn(conf,);
if (num >= vals[conf->valtype].min && num <= vals[conf->valtype].max && (
conf->valtype == Val_bool ||
conf->valtype == Val_colour ||
@@ -145,8 +143,7 @@ config_setl(struct Config *conf, long num) {
void
config_sets(struct Config *conf, char *str) {
- if (!conf)
- return;
+ assert_warn(conf,);
if (conf->valtype != Val_string) {
ui_error("%s must be %s", conf->name, vals[conf->valtype].name);
return;
@@ -163,8 +160,7 @@ config_sets(struct Config *conf, char *str) {
void
config_setr(struct Config *conf, long a, long b) {
- if (!conf)
- return;
+ assert_warn(conf,);
if (a >= vals[conf->valtype].min && b <= vals[conf->valtype].max &&
(conf->valtype == Val_pair || conf->valtype == Val_colourpair)) {
if (conf->pairhandle)
@@ -248,8 +244,7 @@ config_read(char *filename) {
FILE *file;
int save, i;
- if (!filename)
- return -2;
+ assert_warn(filename, -2);
path = realpath(filename, NULL);
diff --git a/src/format.c b/src/format.c
@@ -46,8 +46,7 @@ format_get(struct History *hist) {
char *cmd, *p1, *p2;
int i;
- if (!hist)
- return NULL;
+ assert_warn(hist, NULL);
if (!hist->params)
goto raw;
@@ -163,8 +162,7 @@ format_(struct Window *window, char *format, struct History *hist, int recursive
if (!format)
format = config_gets(format_get(hist));
- if (!format)
- return NULL;
+ assert_warn(format, NULL);
pfree(&ret);
ret = emalloc(rs);
diff --git a/src/handle.c b/src/handle.c
@@ -26,8 +26,7 @@
HANDLER(
handle_PING) {
- if (param_len(msg->params) < 2)
- return;
+ assert_warn(param_len(msg->params) >= 2,);
serv_write(server, "PONG :%s\r\n", *(msg->params+1));
}
@@ -36,8 +35,7 @@ HANDLER(
handle_PONG) {
int len;
- if ((len = param_len(msg->params)) < 2)
- return;
+ assert_warn((len = param_len(msg->params)) >= 2,);
/* RFC1459 says that PONG should have a list of daemons,
* but that's not how PONG seems to work in modern IRC.
@@ -54,8 +52,7 @@ handle_JOIN) {
struct Nick *nick;
char *target;
- if (!msg->from || param_len(msg->params) < 2)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 2,);
target = *(msg->params+1);
if ((chan = chan_get(&server->channels, target, -1)) == NULL)
@@ -86,8 +83,7 @@ handle_PART) {
struct Nick *nick;
char *target;
- if (!msg->from || param_len(msg->params) < 2)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 2,);
target = *(msg->params+1);
if ((chan = chan_get(&server->channels, target, -1)) == NULL)
@@ -118,8 +114,7 @@ handle_KICK) {
struct Nick *nick;
char *target;
- if (!msg->from || param_len(msg->params) < 3)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 3,);
target = *(msg->params+1);
if ((chan = chan_get(&server->channels, target, -1)) == NULL)
@@ -173,8 +168,7 @@ handle_QUIT) {
struct Channel *chan;
struct Nick *nick;
- if (!msg->from || param_len(msg->params) < 1)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 1,);
nick = msg->from;
if (nick_isself(nick)) {
@@ -196,8 +190,7 @@ HANDLER(
handle_MODE) {
struct Channel *chan;
- if (!msg->from || param_len(msg->params) < 3)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 3,);
if (serv_ischannel(server, *(msg->params+1))) {
if ((chan = chan_get(&server->channels, *(msg->params+1), -1)) == NULL)
@@ -221,8 +214,7 @@ handle_PRIVMSG) {
struct Nick *nick;
char *target;
- if (!msg->from || param_len(msg->params) < 3)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 3,);
if (strcmp(*msg->params, "NOTICE") == 0)
act_direct = act_regular = Activity_notice;
@@ -263,8 +255,7 @@ HANDLER(
handle_INVITE) {
struct Channel *query;
- if (!msg->from || param_len(msg->params) < 3)
- return;
+ assert_warn(msg->from && param_len(msg->params) >= 3,);
if ((query = chan_get(&server->queries, msg->from->nick, -1)) != NULL)
hist_addp(query->history, msg, Activity_status, HIST_DFL);
@@ -278,8 +269,7 @@ handle_RPL_ISUPPORT) {
char **params = msg->params;
hist_addp(server->history, msg, Activity_status, HIST_DFL);
- if (param_len(msg->params) < 4)
- return;
+ assert_warn(param_len(msg->params) >= 4,);
params += 2;
@@ -315,8 +305,7 @@ HANDLER(
handle_RPL_CHANNELMODEIS) {
struct Channel *chan;
- if (param_len(msg->params) < 4)
- return;
+ assert_warn(param_len(msg->params) >= 4,);
if ((chan = chan_get(&server->channels, *(msg->params+2), -1)) == NULL)
chan = chan_add(server, &server->channels, *(msg->params+2), 0);
@@ -337,8 +326,7 @@ HANDLER(
handle_RPL_INVITING) {
struct Channel *chan;
- if (param_len(msg->params) < 4)
- return;
+ assert_warn(param_len(msg->params) >= 4,);
if ((chan = chan_get(&server->channels, *(msg->params+3), -1)) == NULL)
chan = chan_add(server, &server->channels, *(msg->params+3), 0);
@@ -355,8 +343,7 @@ handle_RPL_NAMREPLY) {
char **nicks, **nicksref;
char *supportedprivs;
- if (param_len(params) < 5)
- return;
+ assert_warn(param_len(params) >= 5,);
hist_addp(server->history, msg, Activity_status, HIST_LOG);
@@ -403,8 +390,7 @@ handle_RPL_ENDOFNAMES) {
char *target;
hist_addp(server->history, msg, Activity_status, HIST_LOG);
- if (param_len(msg->params) < 3)
- return;
+ assert_warn(param_len(msg->params) >= 3,);
target = *(msg->params+2);
if (strcmp_n(target, expect_get(server, Expect_names)) == 0)
@@ -451,8 +437,7 @@ handle_NICK) {
char *newnick;
char priv;
- if (!msg->from || !*msg->params || !*(msg->params+1))
- return;
+ assert_warn(msg->from && *msg->params && *(msg->params+1),);
nick = msg->from;
hist_addp(server->history, msg, Activity_status, msg->from->self ? HIST_DFL : HIST_LOG);
@@ -486,8 +471,7 @@ HANDLER(
handle_TOPIC) {
struct Channel *chan;
- if (param_len(msg->params) < 3 || !msg->from)
- return;
+ assert_warn(param_len(msg->params) >= 3 && msg->from,);
if ((chan = chan_get(&server->channels, *(msg->params+1), -1)) != NULL) {
hist_addp(chan->history, msg, Activity_status, HIST_DFL);
@@ -501,8 +485,7 @@ handle_RPL_NOTOPIC) {
struct Channel *chan;
char *target;
- if (param_len(msg->params) < 4)
- return;
+ assert_warn(param_len(msg->params) >= 4,);
target = *(msg->params+2);
@@ -523,8 +506,7 @@ handle_RPL_TOPIC) {
struct Channel *chan;
char *target, *topic;
- if (param_len(msg->params) < 4)
- return;
+ assert_warn(param_len(msg->params) >= 4,);
hist_addp(server->history, msg, Activity_status, HIST_LOG);
@@ -551,8 +533,7 @@ handle_RPL_TOPICWHOTIME) {
struct Channel *chan;
char *target;
- if (param_len(msg->params) < 5)
- return;
+ assert_warn(param_len(msg->params) >= 5,);
hist_addp(server->history, msg, Activity_status, HIST_LOG);
diff --git a/src/hirc.h b/src/hirc.h
@@ -30,6 +30,11 @@
#define CONSTLEN(str) ((size_t)((sizeof(str) - sizeof(str[0])) / sizeof(str[0])))
/* compile-time char/wchar_t literals */
#define assert(x) ((void)((x) || (die(1, "assertion '%s' failed at %s:%d in %s()\n", #x, __FILE__, __LINE__, __func__),0)))
+#ifdef ASSERT_ALWAYS_DIES
+#define assert_warn(x,ret) assert(x)
+#else
+#define assert_warn(x,ret) do {if (!(x)) {ui_error("assertion '%s' failed", #x); return ret;}} while(0)
+#endif /* ASSERT_ALWAYS_DIES */
/* strlcpy/wcslcpy.c */
#ifdef HIRC_STRLCPY
diff --git a/src/hist.c b/src/hist.c
@@ -65,8 +65,7 @@ hist_create(struct HistInfo *histinfo, struct Nick *from, char *msg,
struct Nick *np;
char *nick;
- if (!msg)
- return NULL;
+ assert_warn(msg, NULL);
new = emalloc(sizeof(struct History));
new->prev = new->next = NULL;
@@ -129,8 +128,7 @@ hist_add(struct HistInfo *histinfo,
struct tm ptm, ctm, dtm;
int i;
- if (!histinfo || !msg)
- return NULL;
+ assert_warn(histinfo && msg, NULL);
if (options & HIST_MAIN) {
if (options & HIST_TMP && histinfo == main_buf) {
@@ -233,8 +231,7 @@ void
hist_purgeopt(struct HistInfo *histinfo, enum HistOpt options) {
struct History *p, *next;
- if (!histinfo)
- return;
+ assert_warn(histinfo,);
p = histinfo->history;
@@ -361,8 +358,7 @@ hist_loadlog(struct HistInfo *hist, char *server, char *channel) {
size_t len;
struct Nick *from;
- if (!server || !hist)
- return NULL;
+ assert_warn(server && hist, NULL);
if ((logdir = config_gets("log.dir")) == NULL)
return NULL;
diff --git a/src/main.c b/src/main.c
@@ -48,7 +48,11 @@ die(int code, char *format, ...) {
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
+#ifdef DIE_CORE
+ raise(SIGABRT);
+#else
exit(code);
+#endif /* DIE_CORE */
}
}
diff --git a/src/nick.c b/src/nick.c
@@ -132,8 +132,7 @@ struct Nick *
nick_create(char *prefix, char priv, struct Server *server) {
struct Nick *nick;
- if (!prefix || !priv)
- return NULL;
+ assert_warn(prefix && priv, NULL);
nick = emalloc(sizeof(struct Nick));
nick->prefix = estrdup(prefix);
@@ -168,11 +167,10 @@ struct Nick *
nick_add(struct Nick **head, char *prefix, char priv, struct Server *server) {
struct Nick *nick;
- if (!prefix || !priv)
- return NULL;
+ assert_warn(prefix && priv, NULL);
- if ((nick = nick_create(prefix, priv, server)) == NULL)
- return NULL;
+ nick = nick_create(prefix, priv, server);
+ assert_warn(nick, NULL);
nick->next = *head;
nick->prev = NULL;
diff --git a/src/serv.c b/src/serv.c
@@ -90,8 +90,7 @@ serv_create(char *name, char *host, char *port, char *nick, char *username,
struct Server *server;
int i;
- if (!name || !host || !port || !nick)
- return NULL;
+ assert_warn(name && host && port && nick, NULL);
server = emalloc(sizeof(struct Server));
server->prev = server->next = NULL;
@@ -145,8 +144,7 @@ serv_create(char *name, char *host, char *port, char *nick, char *username,
void
serv_update(struct Server *sp, char *nick, char *username,
char *realname, char *password, int tls, int tls_verify) {
- if (!sp)
- return;
+ assert_warn(sp,);
if (nick) {
pfree(&sp->self->nick);
sp->self->nick = estrdup(nick);
@@ -181,8 +179,8 @@ serv_add(struct Server **head, char *name, char *host, char *port, char *nick,
char *username, char *realname, char *password, int tls, int tls_verify) {
struct Server *new, *p;
- if ((new = serv_create(name, host, port, nick, username, realname, password, tls, tls_verify)) == NULL)
- return NULL;
+ new = serv_create(name, host, port, nick, username, realname, password, tls, tls_verify);
+ assert_warn(new, NULL);
if (!*head) {
*head = new;
@@ -201,7 +199,8 @@ struct Server *
serv_get(struct Server **head, char *name) {
struct Server *p;
- if (!head || !*head || !name)
+ assert_warn(head && name, NULL);
+ if (!*head)
return NULL;
for (p = *head; p; p = p->next) {
@@ -216,8 +215,7 @@ int
serv_remove(struct Server **head, char *name) {
struct Server *p;
- if (!head || !name)
- return -1;
+ assert_warn(head && name, -1);
if ((p = serv_get(head, name)) == NULL)
return 0;
@@ -240,8 +238,7 @@ serv_connect(struct Server *server) {
struct addrinfo *ai = NULL;
int fd, ret;
- if (!server)
- return;
+ assert_warn(server,);
if (server->status != ConnStatus_notconnected) {
ui_error("server '%s' is already connected", server->name);
@@ -371,8 +368,7 @@ serv_read(struct Server *sp) {
size_t len;
int ret;
- if (!sp)
- return;
+ assert_warn(sp,);
#ifdef TLS
if (sp->tls) {
@@ -585,8 +581,7 @@ void
support_set(struct Server *server, char *key, char *value) {
struct Support *p;
- if (!server)
- return;
+ assert_warn(server,);
if (!server->supports) {
server->supports = emalloc(sizeof(struct Support));
@@ -615,8 +610,7 @@ int
serv_ischannel(struct Server *server, char *str) {
char *chantypes;
- if (!str || !server)
- return 0;
+ assert_warn(str && server,0);
chantypes = support_get(server, "CHANTYPES");
if (!chantypes)
@@ -631,8 +625,7 @@ serv_auto_add(struct Server *server, char *cmd) {
char **p;
size_t len;
- if (!server || !cmd)
- return;
+ assert_warn(server && cmd,);
if (!server->autocmds) {
len = 1;
@@ -694,8 +687,7 @@ void
schedule_push(struct Server *server, char *tmsg, char *msg) {
struct Schedule *p;
- if (!server)
- return;
+ assert_warn(server,);
if (!server->schedule) {
server->schedule = emalloc(sizeof(struct Schedule));
@@ -719,8 +711,7 @@ schedule_pull(struct Server *server, char *tmsg) {
static char *ret = NULL;
struct Schedule *p;
- if (!server || !tmsg)
- return NULL;
+ assert_warn(server && tmsg, NULL);
for (p = server->schedule; p; p = p->next) {
if (strcmp(p->tmsg, tmsg) == 0) {
diff --git a/src/ui.c b/src/ui.c
@@ -950,8 +950,7 @@ ui_bind(char *binding, char *cmd) {
struct Keybind *p;
char *tmp, *b;
- if (!binding || !cmd)
- return -1;
+ assert_warn(binding && cmd, -1);
b = ui_rectrl(binding);
for (p = keybinds; p; p = p->next)
@@ -982,8 +981,7 @@ ui_unbind(char *binding) {
struct Keybind *p;
char *b;
- if (!binding)
- return -1;
+ assert_warn(binding, -1);
b = ui_rectrl(binding);
for (p=keybinds; p; p = p->next) {