]> PatchyGit - patchyircd.git/commitdiff
output something on every abort()
authorEgor <egor@patchysicp.xyz>
Wed, 28 May 2025 21:20:35 +0000 (00:20 +0300)
committerEgor <egor@patchysicp.xyz>
Wed, 28 May 2025 21:20:35 +0000 (00:20 +0300)
src/irc.cpp
src/job.cpp
src/main.cpp
src/map.h
src/netpoll.cpp
src/util.cpp
src/util.h

index bc14d23..d4a2c91 100644 (file)
@@ -338,7 +338,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                        client->saslbufLen = 0;
                        client->saslexternal = !strcmp(args[0], "EXTERNAL");
                        if (!client->saslbuf)
-                               abort();
+                               abortf("malloc fail (saslbuf)\n");
                } else {
                        size_t len = strlen(args[0]);
                        // there are three cases
@@ -419,7 +419,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                        memset(password, 0, strlen(password));
                                        client->saslusername = mystrdup(username);
                                        if (!client->saslusername)
-                                               abort();
+                                               abortf("strdup fail (saslusername)\n");
                                        goto saslcleanup;
                                } else {
 #ifdef HAS_TLS
@@ -454,7 +454,7 @@ saslcleanup:
                        free(client->nick_reg);
                        client->nick_reg = mystrdup(args[0]);
                        if (!client->nick_reg)
-                               abort();
+                               abortf("strdup fail (nick_reg)\n");
                        newUser(client);
                }
        } else if(strcmp(command, "USER") == 0) {
@@ -471,7 +471,7 @@ saslcleanup:
                free(client->realname);
                client->realname = mystrdup(args[3]);
                if (!client->realname)
-                       abort();
+                       abortf("strdup fail (realname)\n");
                // proceed with registration
                newUser(client);
        } else if(strcmp(command, "PASS") == 0) {
@@ -487,7 +487,8 @@ saslcleanup:
                }
                client->pass_reg = mystrdup(args[0]);
                memset(args[0], 0, strlen(args[0]));
-               if (!client->pass_reg) abort();
+               if (!client->pass_reg)
+                       abortf("strdup fail (pass_reg)\n");
        } else if(strcmp(command, "WEBIRC") == 0) {
                if (is_registered(client))
                        return err_alreadyregistered(client), sendbuf(client), 0;
@@ -1104,7 +1105,8 @@ saslcleanup:
                if (args[0]) {
                        free(client->away);
                        client->away = mystrdup(args[0]);
-                       if (!client->away) abort();
+                       if (!client->away)
+                               abortf("strdup fail (away)\n");
                        rpl_nowaway(client), sendbuf(client);
                } else {
                        free(client->away);
@@ -1304,7 +1306,7 @@ static void updateNuh(struct client_data *client) {
        free(client->nuh);
        client->nuh = (char *)malloc(strlen(n) + strlen(u) + strlen(h) + 3);
        if (!client->nuh)
-               abort();
+               abortf("malloc fail (nuh)\n");
        sprintf(client->nuh, "%s!%s@%s", n, u, h);
        stringlower(client->nuh);
 }
@@ -1359,11 +1361,11 @@ void setNick(struct client_data *client, char* nickname) {
 
        client->nick = mystrdup(nickname);
        if (!client->nick)
-               abort();
+               abortf("strdup fail (nickname)\n");
        updateNuh(client);
        client->key = mystrdup(key);
        if (!client->key)
-               abort();
+               abortf("strdup fail (key)\n");
        client_map.add(client);
        rpl_mononline(0, client), sendbuf_monitor(client->key, 0, 0, 0);
        return;
@@ -1408,14 +1410,14 @@ void client_chghost(struct client_data *client, const char *newusername, const c
                free(client->username);
                client->username = mystrdup(newusername);
                if (!client->username)
-                       abort();
+                       abortf("strdup fail (username)\n");
        }
        // update hostname
        if (newhostname) {
                free(client->hostname);
                client->hostname = mystrdup(newhostname);
                if (!client->hostname)
-                       abort();
+                       abortf("strdup fail (hostname)\n");
        }
        // update nuh
        updateNuh(client);
@@ -1549,15 +1551,18 @@ void channel_join(struct client_data *client, const char *channel, const char *c
 struct channel *channel_new(const char *channel, const char *key, time_t creation)
 {
        struct channel *chan = (struct channel *)malloc(sizeof(struct channel));
-       if (!chan) abort();
+       if (!chan)
+               abortf("malloc fail (channel)\n");
        memset(chan, 0, sizeof(struct channel));
        chan->creation = creation;
        chan->member_limit = -1;
        chan->dmode = DMODE_NOEXTERNAL | DMODE_TOPICPROT | DMODE_PERSIST;
        chan->key = mystrdup(key);
-       if (!chan->key) abort();
+       if (!chan->key)
+               abortf("strdup fail (key)\n");
        chan->name = mystrdup(channel);
-       if (!chan->name) abort();
+       if (!chan->name)
+               abortf("strdup fail (name)\n");
        return chan;
 }
 void channel_part(struct client_data *client, struct channel *chan, const char *reason, struct client_data *kicker)
@@ -1678,7 +1683,7 @@ void channel_mode(struct client_data *client, struct channel *chan, char *args[]
                                        free(chan->chankey);
                                chan->chankey = mystrdup(*args);
                                if (!chan->chankey)
-                                       abort();
+                                       abortf("strdup fail (chankey)\n");
                        } else {
                                free(chan->chankey);
                                chan->chankey = NULL;
@@ -1777,9 +1782,11 @@ void channel_topic(struct client_data *src, struct channel *chan, const char *to
                chan->topic_when = 0;
        } else {
                chan->topic = mystrdup(topic);
-               if (!chan->topic) abort();
+               if (!chan->topic)
+                       abortf("strdup fail (topic)\n");
                chan->topic_who = mystrdup(who);
-               if (!chan->topic_who) abort();
+               if (!chan->topic_who)
+                       abortf("strdup fail (topic_who)\n");
                chan->topic_when = when;
        }
        if (src)
index 91e34a6..04c262e 100644 (file)
@@ -90,7 +90,7 @@ static void job_process(void)
        if (joblen && jobtab[0].job) {
                pid_t pid = fork();
                if (pid == -1) {
-                       abort();
+                       abortf("job_process: fork failed\n");
                } else if (pid == 0) {
                        int rv = jobtab[0].job(jobtab[0].client, jobtab[0].data, jobtab[0].len);
                        memset(jobtab[0].data, 0, jobtab[0].len);
@@ -135,7 +135,7 @@ void job_add(struct client_data *cl, jobproc_t job, contproc_t then, char *data,
                jobcap = jobcap == 0 ? 16 : jobcap * 2;
                jobtab = (struct jobent *)realloc(jobtab, sizeof(struct jobent)*jobcap);
                if (!jobtab)
-                       abort();
+                       abortf("job_add: allocation failure\n");
        }
        jobtab[joblen++] = ent;
 }
@@ -180,7 +180,7 @@ void job_password_check(struct client_data *client, const char *key, const char
                size_t len = pwlen+strlen(salt)+1;
                char *buf = (char *)malloc(len);
                if (!buf)
-                       abort();
+                       abortf("job_password_check: allocation failure\n");
                memcpy(buf, pw, pwlen);
                memcpy(buf+pwlen, salt, len-pwlen);
                job_add(client, job_password_check_job, then, buf, len);
index faa4af5..66921ec 100644 (file)
@@ -156,7 +156,8 @@ static void accept_from_sock(struct listener *listener)
                cl->cctx = cctx;
                cl->type = addr.ss_family == AF_INET6 ? 6 : 4;
                cl->peername = mystrdup(client_addr);
-               if (!cl->peername) abort();
+               if (!cl->peername)
+                       abortf("strdup fail (peername)\n");
                cl->peerport = port;
                if (cctx)
                        cl->umode |= UMODE_SECURE;
@@ -167,7 +168,7 @@ static void accept_from_sock(struct listener *listener)
                        cl->hostname = mystrdup(hostname);
                }
                if (!cl->hostname)
-                       abort();
+                       abortf("strdup fail (hostname)\n");
                netpoll::add(cl->FD, SOCKTYPE_CLIENT, cl);
                update_poll_reg(cl);
                copy_strf(&databuf, ":%s NOTICE * :Hello welcome to PatchyIRCD!\r\n", hostname);
@@ -292,7 +293,7 @@ int main(int argc, char *argv[])
                fake->umode |= UMODE_OPER | UMODE_SECURE;
                fake->hostname = mystrdup("localhost");
                if (!fake->hostname)
-                       abort();
+                       abortf("strdup fail (hostname)\n");
                strcpy_lit(fake->buf, "NICK :root\r\n");
                fake->bufLen = strlen(fake->buf);
                parsePacket(fake);
@@ -320,7 +321,7 @@ int main(int argc, char *argv[])
                                        if (memcmp(&fake->buf[1], fake->nick, nlen) != 0) {
                                                char *nnick = (char *)malloc(nlen+1);
                                                if (!nnick)
-                                                       abort();
+                                                       abortf("malloc fail (fake->nick)\n");
                                                memcpy(nnick, &fake->buf[1], nlen);
                                                nnick[nlen] = '\0';
                                                free(fake->nick);
index 9dd1d8c..90f6e55 100644 (file)
--- a/src/map.h
+++ b/src/map.h
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <utility>
 #include <iterator>
+#include "util.h"
 
 /* A map abstract data type with following properties:
  * - keys are lexicographically-sorted unique NUL-terminated strings
@@ -95,7 +96,7 @@ public:
        bool add(V v) {
                void *old;
                if (_set(T::to_key(v), v, &old) == -1)
-                       abort();
+                       abortf("map_base2::add: allocation failure\n");
                return !old;
        }
        bool remove(V v) {
@@ -113,7 +114,7 @@ struct client_data;
 struct clientmap : map_base2<clientmap, struct client_data *> {
        void rename(struct client_data *v, const char *newk) {
                if (remove(v) && _set(newk, v, 0) == -1)
-                       abort();
+                       abortf("clientmap::rename: allocation failure\n");
        }
 };
 struct channel;
index 06c3574..c2e98dc 100644 (file)
@@ -1,4 +1,5 @@
 #include "netpoll.h"
+#include "util.h"
 
 #ifdef HAS_UV
 #include <uv.h>
@@ -81,10 +82,8 @@ namespace uvalloc {
        static polldata *get() {
                polldata *p = freelist;
                freelist = static_cast<polldata *>(p->uv.data);
-               if (!p) {
-                       fprintf(stderr, "uvalloc::get: out of polldata!\n");
-                       abort();
-               }
+               if (!p)
+                       abortf("uvalloc::get: out of polldata!\n");
                return p;
        }
        static void put(polldata *p) {
@@ -199,19 +198,13 @@ static int search(SOCKET fd) {
 
 void add(SOCKET fd, int type, void *assoc)
 {
-       if (fd == INVALID_SOCKET) {
-               fprintf(stderr, "netpoll::add: INVALID_SOCKET\n");
-               abort();
-       }
+       if (fd == INVALID_SOCKET)
+               abortf("netpoll::add: INVALID_SOCKET\n");
        int i = search(fd);
-       if (IS_FOUND(i, fd)) {
-               fprintf(stderr, "netpoll::add: socket already added\n");
-               abort();
-       }
-       if (nfds == NETPOLL_MAXFDS) {
-               fprintf(stderr, "netpoll::add: NETPOLL_MAXFDS limit\n");
-               abort();
-       }
+       if (IS_FOUND(i, fd))
+               abortf("netpoll::add: socket already added\n");
+       if (nfds == NETPOLL_MAXFDS)
+               abortf("netpoll::add: NETPOLL_MAXFDS limit\n");
        memmove(fds+i+1, fds+i, sizeof(fds[0])*(nfds-i));
        nfds++;
        fds[i] = (struct net_pollfd){ .assoc = assoc, .fd = fd, .type = type };
@@ -219,15 +212,11 @@ void add(SOCKET fd, int type, void *assoc)
 }
 void del_and_close(SOCKET fd)
 {
-       if (fd == INVALID_SOCKET) {
-               fprintf(stderr, "netpoll::del_and_close: INVALID_SOCKET\n");
-               abort();
-       }
+       if (fd == INVALID_SOCKET)
+               abortf("netpoll::del_and_close: INVALID_SOCKET\n");
        int i = search(fd);
-       if (!IS_FOUND(i, fd)) {
-               fprintf(stderr, "netpoll::del_and_close: socket not added\n");
-               abort();
-       }
+       if (!IS_FOUND(i, fd))
+               abortf("netpoll::del_and_close: socket not added\n");
        reg_impl(i, 0);
        del_impl(i);
        memmove(fds+i, fds+i+1, sizeof(fds[0])*(nfds-(i+1)));
@@ -238,28 +227,20 @@ void del_and_close(SOCKET fd)
 }
 void reg(SOCKET fd, int ev)
 {
-       if (fd == INVALID_SOCKET) {
-               fprintf(stderr, "netpoll::reg: INVALID_SOCKET\n");
-               abort();
-       }
+       if (fd == INVALID_SOCKET)
+               abortf("netpoll::reg: INVALID_SOCKET\n");
        int i = search(fd);
-       if (!IS_FOUND(i, fd)) {
-               fprintf(stderr, "netpoll::reg: socket not added\n");
-               abort();
-       }
+       if (!IS_FOUND(i, fd))
+               abortf("netpoll::reg: socket not added\n");
        reg_impl(i, ev);
 }
 void change_type(SOCKET fd, int type)
 {
-       if (fd == INVALID_SOCKET) {
-               fprintf(stderr, "netpoll::change_type: INVALID_SOCKET\n");
-               abort();
-       }
+       if (fd == INVALID_SOCKET)
+               abortf("netpoll::change_type: INVALID_SOCKET\n");
        int i = search(fd);
-       if (!IS_FOUND(i, fd)) {
-               fprintf(stderr, "netpoll::change_type: socket not added\n");
-               abort();
-       }
+       if (!IS_FOUND(i, fd))
+               abortf("netpoll::change_type: socket not added\n");
        fds[i].type = type;
 }
 }
index 2441f03..8669a62 100644 (file)
@@ -7,7 +7,8 @@ static void append_strn(struct dynarr *d, const char *s, int len){
                while (d->len + len > d->cap)
                        d->cap *= 2;
                char *ndata = (char *)realloc(d->buf, d->cap);
-               if(!ndata) abort();
+               if(!ndata)
+                       abortf("append_strn: allocation failure\n");
                d->buf = ndata;
        }
        memcpy(d->buf + d->len, s, len);
@@ -163,7 +164,7 @@ const char *getdatafile(const char *fn)
 {
        static char buf[256];
        if (!getdirfile_r(buf, sizeof(buf), datadir, fn))
-               abort();
+               abortf("getdatafile: filename too long\n");
        return buf;
 }
 
@@ -179,6 +180,6 @@ const char *getluafile(const char *fn)
 {
        static char buf[256];
        if (!getdirfile_r(buf, sizeof(buf), luadir, fn))
-               abort();
+               abortf("getluafile: filename too long\n");
        return buf;
 }
index 5749f89..9f693c0 100644 (file)
@@ -15,12 +15,14 @@ void append_strf(struct dynarr *d, const char *s, ...);
 #define copy_strf(d, ...) append_strf(((d)->len=0, (d)), __VA_ARGS__)
 void stringupper(char *s);
 void stringlower(char *s);
+#define abortf(...) do { fprintf(stderr, "(abort) " __VA_ARGS__); abort(); } while(0)
 #ifndef _MSC_VER
 #pragma GCC poison strdup
 #pragma GCC poison strcpy
 #pragma GCC poison strncpy
 #pragma GCC poison strcat
 #pragma GCC poison strncat
+#pragma GCC poison abort
 #endif
 #define strcpy_lit(a, b) memcpy((a), (b), sizeof(b))
 char *mystrdup(const char *s);