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
memset(password, 0, strlen(password));
client->saslusername = mystrdup(username);
if (!client->saslusername)
- abort();
+ abortf("strdup fail (saslusername)\n");
goto saslcleanup;
} else {
#ifdef HAS_TLS
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) {
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) {
}
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;
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);
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);
}
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;
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);
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)
free(chan->chankey);
chan->chankey = mystrdup(*args);
if (!chan->chankey)
- abort();
+ abortf("strdup fail (chankey)\n");
} else {
free(chan->chankey);
chan->chankey = NULL;
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)
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);
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;
}
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);
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;
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);
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);
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);
#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
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) {
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;
#include "netpoll.h"
+#include "util.h"
#ifdef HAS_UV
#include <uv.h>
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) {
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 };
}
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)));
}
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;
}
}
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);
{
static char buf[256];
if (!getdirfile_r(buf, sizeof(buf), datadir, fn))
- abort();
+ abortf("getdatafile: filename too long\n");
return buf;
}
{
static char buf[256];
if (!getdirfile_r(buf, sizeof(buf), luadir, fn))
- abort();
+ abortf("getluafile: filename too long\n");
return buf;
}
#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);