diff --git a/src/bip.c b/src/bip.c index 7a52c26..c89118e 100644 --- a/src/bip.c +++ b/src/bip.c @@ -75,7 +75,7 @@ static void hash_binary(char *hex, unsigned char **password, unsigned int *seed) if (strlen(hex) != 40) fatal("Incorrect password format %s\n", hex); - md5 = malloc(20); + md5 = bip_malloc(20); for (i = 0; i < 20; i++) { sscanf(hex + 2 * i, "%02x", &buf); md5[i] = buf; @@ -133,7 +133,7 @@ void conf_die(bip_t *bip, char *fmt, ...) va_list ap; int size = ERRBUFSZ; int n; - char *error = malloc(size); + char *error = bip_malloc(size); for (;;) { va_start(ap, fmt); @@ -1201,7 +1201,7 @@ int main(int argc, char **argv) return 0; } #ifdef HAVE_OIDENTD - bip.oidentdpath = malloc(strlen(home) + 1 + + bip.oidentdpath = bip_malloc(strlen(home) + 1 + strlen(OIDENTD_FILENAME) + 1); strcpy(bip.oidentdpath, home); strcat(bip.oidentdpath, "/"); @@ -1210,13 +1210,13 @@ int main(int argc, char **argv) if (!conf_biphome) { - conf_biphome = malloc(strlen(home) + strlen("/.bip") + 1); + conf_biphome = bip_malloc(strlen(home) + strlen("/.bip") + 1); strcpy(conf_biphome, home); strcat(conf_biphome, "/.bip"); } if (!confpath) { - confpath = malloc(strlen(conf_biphome) + 1 + + confpath = bip_malloc(strlen(conf_biphome) + 1 + strlen(S_CONF) + 1); *confpath = 0; strcat(confpath, conf_biphome); @@ -1234,14 +1234,16 @@ int main(int argc, char **argv) if (!conf_log_root) { char *ap = "/logs"; - conf_log_root = malloc(strlen(conf_biphome) + strlen(ap) + 1); + conf_log_root = bip_malloc(strlen(conf_biphome) + + strlen(ap) + 1); strcpy(conf_log_root, conf_biphome); strcat(conf_log_root, ap); mylog(LOG_INFO, "Default log root: %s", conf_log_root); } if (!conf_pid_file) { char *pid = "/bip.pid"; - conf_pid_file = malloc(strlen(conf_biphome) + strlen(pid) + 1); + conf_pid_file = bip_malloc(strlen(conf_biphome) + + strlen(pid) + 1); strcpy(conf_pid_file, conf_biphome); strcat(conf_pid_file, pid); mylog(LOG_INFO, "Default pid file: %s", conf_pid_file); @@ -1254,7 +1256,7 @@ int main(int argc, char **argv) if (!conf_ssl_certfile) { char *ap = "/bip.pem"; - conf_ssl_certfile = malloc(strlen(conf_biphome) + + conf_ssl_certfile = bip_malloc(strlen(conf_biphome) + strlen(ap) + 1); strcpy(conf_ssl_certfile, conf_biphome); strcat(conf_ssl_certfile, ap); @@ -2082,7 +2084,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, } line->elemv = realloc(line->elemv, (line->elemc + 1) * sizeof(char *)); - line->elemv[line->elemc] = malloc(slen + 1); + line->elemv[line->elemc] = bip_malloc(slen + 1); memcpy(line->elemv[line->elemc], ptr, slen); line->elemv[line->elemc][slen] = 0; line->elemc++; @@ -2094,7 +2096,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, line->elemv = realloc(line->elemv, (line->elemc + 1) * sizeof(char *)); slen = eptr - ptr; - line->elemv[line->elemc] = malloc(slen + 1); + line->elemv[line->elemc] = bip_malloc(slen + 1); memcpy(line->elemv[line->elemc], ptr, slen); line->elemv[line->elemc][slen] = 0; line->elemc++; diff --git a/src/conf.y b/src/conf.y index d43b9d4..28a005d 100644 --- a/src/conf.y +++ b/src/conf.y @@ -20,6 +20,8 @@ extern int yylex (void); extern char *yytext; extern int linec; +#define YYMALLOC bip_malloc + int yywrap() { return 1; diff --git a/src/connection.c b/src/connection.c index 0829851..fca4235 100644 --- a/src/connection.c +++ b/src/connection.c @@ -479,7 +479,7 @@ static void data_find_lines(connection_t *cn) if (ssz >= 1) { if (p[len - 1] == '\r') ssz--; - buf = malloc(ssz + 1); + buf = bip_malloc(ssz + 1); memcpy(buf, p + lastlen, ssz); buf[ssz] = 0; @@ -891,7 +891,7 @@ static void create_socket(char *dsthostname, char *dstport, char *srchostname, cn->connected = CONN_ERROR; cdata = (struct connecting_data *) - malloc(sizeof(struct connecting_data)); + bip_malloc(sizeof(struct connecting_data)); if (!cdata) fatal("Out of memory."); cdata->dst = cdata->src = cdata->cur = NULL; @@ -998,7 +998,7 @@ static connection_t *connection_init(int anti_flood, int ssl, int timeout, list_t *outgoing; conn = (connection_t *)calloc(sizeof(connection_t), 1); - incoming = (char*)malloc(sizeof(char) * CONN_BUFFER_SIZE); + incoming = (char *)bip_malloc(CONN_BUFFER_SIZE); outgoing = list_new(NULL); conn->anti_flood = anti_flood; @@ -1586,9 +1586,7 @@ static char *socket_ip(int fd, int remote) return NULL; } - ip = malloc(65); - if (ip == NULL) - fatal("malloc"); + ip = bip_malloc(65); switch (addr.sa_family) { case AF_INET: diff --git a/src/irc.c b/src/irc.c index 4dc8157..610d3e5 100644 --- a/src/irc.c +++ b/src/irc.c @@ -97,9 +97,7 @@ char *nick_from_ircmask(char *mask) if (!*nick) return strdup(mask); len = nick - mask; - ret = malloc(len + 1); - if (!ret) - fatal("malloc"); + ret = bip_malloc(len + 1); memcpy(ret, mask, len); ret[len] = 0; return ret; @@ -115,7 +113,7 @@ list_t *channel_name_list(struct channel *c) ret = list_new(NULL); size_t len = 0; - char *str = malloc(NAMESIZE); + char *str = bip_malloc(NAMESIZE); *str = 0; for (hash_it_init(&c->nicks, &hi); hash_it_item(&hi); hash_it_next(&hi)){ @@ -127,7 +125,7 @@ list_t *channel_name_list(struct channel *c) if (len + strlen(n->name) + 2 + (n->ovmask ? 1 : 0) >= NAMESIZE) { list_add_last(ret, str); - str = malloc(s); + str = bip_malloc(s); *str = 0; len = 0; } @@ -247,7 +245,7 @@ static void irc_server_connected(struct link_server *server) for (list_it_init(&LINK(server)->on_connect_send, &itocs); list_it_item(&itocs); list_it_next(&itocs)) { ssize_t len = strlen(list_it_item(&itocs)) + 2; - char *str = malloc(len + 1); + char *str = bip_malloc(len + 1); sprintf(str, "%s\r\n", (char *)list_it_item(&itocs)); write_line(CONN(server), str); free(str); @@ -393,7 +391,7 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, } else if (strcmp(line->elemv[0], "433") == 0) { if (LINK(server)->s_state != IRCS_CONNECTED) { size_t nicklen = strlen(server->nick); - char *newnick = malloc(nicklen + 2); + char *newnick = bip_malloc(nicklen + 2); strcpy(newnick, server->nick); if (strlen(server->nick) < 9) { strcat(newnick, "`"); @@ -518,7 +516,7 @@ static void irc_send_join(struct link_client *ic, struct channel *chan) if (!user) fatal("irc_send_join: No user associated"); - ircmask = malloc(strlen(LINK(ic)->l_server->nick) + + ircmask = bip_malloc(strlen(LINK(ic)->l_server->nick) + strlen("!bip@bip.bip.bip") + 1); strcpy(ircmask, LINK(ic)->l_server->nick); strcat(ircmask, "!bip@bip.bip.bip"); @@ -646,7 +644,7 @@ static char *get_str_elem(char *str, int num) } if (c - cur < 1) return NULL; - ret = malloc(c - cur + 1); + ret = bip_malloc(c - cur + 1); strncpy(ret, cur, c - cur); ret[c - cur] = 0; return ret; @@ -655,7 +653,7 @@ static char *get_str_elem(char *str, int num) c = str + strlen(str); if (c - cur < 1) return NULL; - ret = malloc(c - cur + 1); + ret = bip_malloc(c - cur + 1); strncpy(ret, cur, c - cur); ret[c - cur] = 0; return ret; @@ -1009,7 +1007,7 @@ static int irc_cli_join(struct link_client *irc, struct line *line) while ((e = strchr(s, ','))) { size_t len = e - s; - char *p = malloc(len + 1); + char *p = bip_malloc(len + 1); size_t klen; char *kp = NULL; @@ -1021,7 +1019,7 @@ static int irc_cli_join(struct link_client *irc, struct line *line) if (!ke) ke = ks + strlen(ks); klen = ke - ks; - kp = malloc(klen + 1); + kp = bip_malloc(klen + 1); memcpy(kp, ks, klen); kp[klen] = 0; if (*ke == 0) @@ -1168,7 +1166,7 @@ static void irc_copy_cli(struct link_client *src, struct link_client *dest, if (len == 0) return; - tmp = malloc(len); + tmp = bip_malloc(len); snprintf(tmp, len, " -> %s", line->elemv[2]); tmp[len - 1] = 0; @@ -1398,15 +1396,11 @@ static int irc_353(struct link_server *server, struct line *line) eon++; len = eon - names; - tmp = malloc(len + 1); - if (!tmp) - fatal("malloc"); + tmp = bip_malloc(len + 1); memcpy(tmp, names, len); tmp[len] = 0; - nick = malloc(sizeof(struct nick)); - if (!nick) - fatal("malloc"); + nick = bip_malloc(sizeof(struct nick)); nick->name = tmp; nick->ovmask = ovmask; @@ -1698,7 +1692,7 @@ static int irc_mode(struct link_server *server, struct line *line) static char *irc_timestamp(void) { - char *ts = malloc(21); + char *ts = bip_malloc(21); snprintf(ts, 20, "%ld", (long int)time(NULL)); return ts; } @@ -2191,10 +2185,7 @@ void oidentd_dump(bip_t *bip) return; } - content = (char *)malloc(stats.st_size + 1); - - if (content == NULL) - fatal("out of memory"); + content = (char *)bip_malloc(stats.st_size + 1); if (fread(content, 1, stats.st_size, f) != (size_t)stats.st_size) { diff --git a/src/lex.l b/src/lex.l index 97f04c1..027844f 100644 --- a/src/lex.l +++ b/src/lex.l @@ -115,9 +115,7 @@ list_t *parse_conf(FILE *file, int *err) "client_side_ssl_pem" { return LEX_CSS_PEM; } \"[^"]*\" { size_t len = strlen(yytext) - 2; - yylval.string = malloc(len + 1); - if (!yylval.string) - fatal("malloc"); + yylval.string = bip_malloc(len + 1); memcpy(yylval.string, yytext + 1, len); yylval.string[len] = 0; return LEX_STRING; diff --git a/src/line.c b/src/line.c index 999da6f..662d1e9 100644 --- a/src/line.c +++ b/src/line.c @@ -22,9 +22,7 @@ void irc_line_init(struct line *l) struct line *irc_line_new() { struct line *l; - l = malloc(sizeof(struct line)); - if (!l) - fatal("malloc"); + l = bip_malloc(sizeof(struct line)); irc_line_init(l); return l; } @@ -53,7 +51,7 @@ struct line *irc_line_dup(struct line *line) struct line *nl = irc_line_new(); nl->origin = line->origin ? strdup(line->origin) : NULL; nl->elemc = line->elemc; - nl->elemv = malloc(sizeof(char *) * line->elemc); + nl->elemv = bip_malloc(sizeof(char *) * line->elemc); for (i = 0; i < line->elemc; i++) nl->elemv[i] = strdup(line->elemv[i]); nl->colon = line->colon; @@ -86,7 +84,7 @@ char *irc_line_to_string(struct line *l) len += strlen(l->elemv[i]) + 1; len += 1; /* remove one trailing space and add \r\n */ len++; /* last args ":" */ - ret = malloc(len + 1); + ret = bip_malloc(len + 1); ret[0] = 0; if (l->origin) { @@ -127,9 +125,7 @@ struct line *irc_line(char *str) if (!*space) return NULL; len = space - str - 1; /* leading ':' */ - line->origin = malloc(len + 1); - if (!line->origin) - fatal("malloc"); + line->origin = bip_malloc(len + 1); memcpy(line->origin, str + 1, len); line->origin[len] = 0; str = space; @@ -158,9 +154,7 @@ struct line *irc_line(char *str) space++; } len = space - str; - tmp = line->elemv[curelem] = malloc(len + 1); - if (!tmp) - fatal("malloc"); + tmp = line->elemv[curelem] = bip_malloc(len + 1); memcpy(tmp, str, len); tmp[len] = 0; if (curelem == 0) diff --git a/src/log.c b/src/log.c index e37ac84..a0c8e86 100644 --- a/src/log.c +++ b/src/log.c @@ -76,7 +76,7 @@ int check_dir_r(char *dirname) mylog(LOG_DEBUGVERB, "Recursive check of %s engaged", dirname); tmp = dirname; - dir = (char *)malloc(sizeof(char) * (len + 1)); + dir = (char *)bip_malloc(len + 1); while (*tmp) { int slash_ok = 1; while (*tmp == '/') { @@ -140,9 +140,7 @@ char *log_build_filename(log_t *logdata, char *destination) strtolower(dest); log_format_len = strlen(conf_log_format); - logfile = (char*)malloc((MAX_PATH_LEN + 1)*sizeof(char)); - if (!logfile) - fatal("out of memory"); + logfile = (char *)bip_malloc(MAX_PATH_LEN + 1); time(&s); now = localtime(&s); @@ -232,9 +230,7 @@ static int log_add_file(log_t *logdata, char *destination, char *filename) return 0; } - lf = malloc(sizeof(logfile_t)); - if (!lf) - fatal("out of memory"); + lf = bip_malloc(sizeof(logfile_t)); lf->file = f; lf->filename = strdup(filename); if (!lf->filename) @@ -557,7 +553,7 @@ void log_init_topic_time(log_t *logdata, char *channel, char *who, char *when) seconds = atoi(when); time = localtime(&seconds); - timestr = (char*)malloc(sizeof(char) * (50 + 1)); + timestr = (char *)bip_malloc(50 + 1); timestr[0] = '\0'; if (time) strftime(timestr, 50, "%A %d %B %Y, %H:%M:%S", time); @@ -574,8 +570,8 @@ void log_mode(log_t *logdata, char *ircmask, char *channel, char *modes, char **modargv, unsigned modargc) { unsigned i; - char *tmpbuf = malloc(LOGLINE_MAXLEN + 1); - char *tmpbuf2 = malloc(LOGLINE_MAXLEN + 1); + char *tmpbuf = bip_malloc(LOGLINE_MAXLEN + 1); + char *tmpbuf2 = bip_malloc(LOGLINE_MAXLEN + 1); char *tmp; snprintf(tmpbuf, LOGLINE_MAXLEN, "%s -!- mode/%s [%s", timestamp(), channel, modes); @@ -853,11 +849,9 @@ char *log_beautify(log_t *logdata, char *buf, char *dest) if (lom == 0) return _log_wrap(dest, buf); - p = ret = (char *)malloc( + p = ret = (char *)bip_malloc( 1 + lon + strlen(LAMESTRING) + lod + 2 + lots + 2 + lom + 3 + action * (2 + strlen("ACTION ")) + out * strlen(PMSG_ARROW)); - if (!p) - fatal("out of memory"); *p++ = ':'; @@ -947,7 +941,7 @@ char *log_backread(log_t *logdata, char *destination, int *skip) return NULL; } - buf = (char *)malloc((LOGLINE_MAXLEN + 1) * sizeof(char)); + buf = (char *)bip_malloc(LOGLINE_MAXLEN + 1); next_file: /* check the files containing data to backlog */ @@ -1090,7 +1084,7 @@ static char *_log_wrap(char *dest, char *line) char *buf; size_t count; - buf = malloc(LOGLINE_MAXLEN + 1); + buf = bip_malloc(LOGLINE_MAXLEN + 1); count = snprintf(buf, LOGLINE_MAXLEN + 1, ":" P_IRCMASK " PRIVMSG %s :%s\r\n", dest, line); if (count >= LOGLINE_MAXLEN + 1) { @@ -1186,7 +1180,7 @@ log_t *log_new(struct user *user, char *network) logdata->user = user; logdata->network = strdup(network); hash_init(&logdata->logfgs, HASH_NOCASE); - logdata->buffer = (char *)malloc((LOGLINE_MAXLEN + 1) * sizeof(char)); + logdata->buffer = (char *)bip_malloc(LOGLINE_MAXLEN + 1); logdata->buffer[LOGLINE_MAXLEN - 1] = 0; // debug logdata->buffer[LOGLINE_MAXLEN] = 0; if (!logdata->user || !logdata->network || !logdata->buffer) diff --git a/src/md5.c b/src/md5.c index 97b6170..50b2819 100644 --- a/src/md5.c +++ b/src/md5.c @@ -380,14 +380,14 @@ unsigned char *chash_double(char *str, unsigned int seed) length = strlen(str); length += 4; - ptr = malloc(length); + ptr = bip_malloc(length); ptr[0] = seed >> 24 & 0xff; ptr[1] = seed >> 16 & 0xff; ptr[2] = seed >> 8 & 0xff; ptr[3] = seed & 0xff; memcpy(ptr + 4, str, length - 4); - md5 = malloc(16 + 4); + md5 = bip_malloc(16 + 4); memcpy(md5, ptr, 4); md5_starts(&ctx); diff --git a/src/util.c b/src/util.c index 139b538..d8b613a 100644 --- a/src/util.c +++ b/src/util.c @@ -27,6 +27,17 @@ extern int conf_log_level; extern int conf_log_system; extern int errno; +extern FILE *conf_global_log_file; + +void *bip_malloc(size_t size) +{ + void *r = malloc(size); + if (!r) { + fprintf(conf_global_log_file, 1, strlen("malloc"), "malloc"); + exit(28); + } + return r; +} /* * ::= { | | } @@ -104,8 +115,6 @@ char *checkmode2text(int v) } #endif -extern FILE *conf_global_log_file; - void _mylog(int level, char *fmt, va_list ap) { char *prefix; @@ -196,9 +205,7 @@ void list_init(list_t *l, int (*cmp)(void *, void *)) list_t *list_new(int (*cmp)(void *, void *)) { list_t *l; - l = malloc(sizeof(list_t)); - if (!l) - fatal("malloc"); + l = bip_malloc(sizeof(list_t)); list_init(l, cmp); return l; } @@ -206,9 +213,7 @@ list_t *list_new(int (*cmp)(void *, void *)) static struct list_item *list_item(void *ptr) { struct list_item *l; - l = malloc(sizeof(struct list_item)); - if (!l) - fatal("malloc"); + l = bip_malloc(sizeof(struct list_item)); l->ptr = ptr; l->next = NULL; l->prev = NULL; @@ -475,9 +480,7 @@ void hash_free(hash_t *h) hash_t *hash_new(int options) { hash_t *h; - h = malloc(sizeof(hash_t)); - if (!h) - fatal("malloc"); + h = bip_malloc(sizeof(hash_t)); hash_init(h, options); return h; } @@ -500,9 +503,7 @@ void hash_insert(hash_t *hash, char *key, void *ptr) if (hash_get(hash, key)) fatal("Element with key %s already in hash %x\n", key, hash); - it = malloc(sizeof(struct hash_item)); - if (!it) - fatal("malloc"); + it = bip_malloc(sizeof(struct hash_item)); it->key = strdup(key); it->item = ptr; list_add_first(&hash->lists[hash_func(key)], it); diff --git a/src/util.h b/src/util.h index dbface7..8eb461c 100644 --- a/src/util.h +++ b/src/util.h @@ -123,5 +123,6 @@ char *hrtime(time_t t); char *checkmode2text(int v); #endif #define bool2text(v) ((v) ? "true" : "false") +void *bip_malloc(size_t size); #endif