1
0
forked from bip/bip

calloc/realloc checks.

This commit is contained in:
Arnaud Cornet 2008-12-11 11:00:05 +01:00
parent 2b96805ecb
commit e18d335578
9 changed files with 48 additions and 74 deletions

View File

@ -147,7 +147,7 @@ void conf_die(bip_t *bip, char *fmt, ...)
size = n + 1; size = n + 1;
else else
size *= 2; size *= 2;
error = realloc(error, size); error = bip_realloc(error, size);
} }
va_start(ap, fmt); va_start(ap, fmt);
_mylog(LOG_ERROR, fmt, ap); _mylog(LOG_ERROR, fmt, ap);
@ -341,7 +341,7 @@ static int add_network(bip_t *bip, list_t *data)
n->serverv = NULL; n->serverv = NULL;
n->serverc = 0; n->serverc = 0;
} else { } else {
n = calloc(sizeof(struct network), 1); n = bip_calloc(sizeof(struct network), 1);
hash_insert(&bip->networks, name, n); hash_insert(&bip->networks, name, n);
} }
@ -356,7 +356,7 @@ static int add_network(bip_t *bip, list_t *data)
break; break;
#endif #endif
case LEX_SERVER: case LEX_SERVER:
n->serverv = realloc(n->serverv, (n->serverc + 1) n->serverv = bip_realloc(n->serverv, (n->serverc + 1)
* sizeof(struct server)); * sizeof(struct server));
n->serverc++; n->serverc++;
memset(&n->serverv[n->serverc - 1], 0, memset(&n->serverv[n->serverc - 1], 0,
@ -667,7 +667,7 @@ static int add_user(bip_t *bip, list_t *data, struct historical_directives *hds)
} }
u = hash_get(&bip->users, name); u = hash_get(&bip->users, name);
if (!u) { if (!u) {
u = calloc(sizeof(struct user), 1); u = bip_calloc(sizeof(struct user), 1);
hash_insert(&bip->users, name, u); hash_insert(&bip->users, name, u);
hash_init(&u->connections, HASH_NOCASE); hash_init(&u->connections, HASH_NOCASE);
u->admin = 0; u->admin = 0;
@ -2073,7 +2073,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line,
ptr++; ptr++;
continue; continue;
} }
line->elemv = realloc(line->elemv, line->elemv = bip_realloc(line->elemv,
(line->elemc + 1) * sizeof(char *)); (line->elemc + 1) * sizeof(char *));
line->elemv[line->elemc] = bip_malloc(slen + 1); line->elemv[line->elemc] = bip_malloc(slen + 1);
memcpy(line->elemv[line->elemc], ptr, slen); memcpy(line->elemv[line->elemc], ptr, slen);
@ -2084,7 +2084,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line,
eptr = ptr + strlen(ptr); eptr = ptr + strlen(ptr);
slen = eptr - ptr; slen = eptr - ptr;
if (slen != 0) { if (slen != 0) {
line->elemv = realloc(line->elemv, line->elemv = bip_realloc(line->elemv,
(line->elemc + 1) * sizeof(char *)); (line->elemc + 1) * sizeof(char *));
slen = eptr - ptr; slen = eptr - ptr;
line->elemv[line->elemc] = bip_malloc(slen + 1); line->elemv[line->elemc] = bip_malloc(slen + 1);

View File

@ -997,7 +997,7 @@ static connection_t *connection_init(int anti_flood, int ssl, int timeout,
char *incoming; char *incoming;
list_t *outgoing; list_t *outgoing;
conn = (connection_t *)calloc(sizeof(connection_t), 1); conn = (connection_t *)bip_calloc(sizeof(connection_t), 1);
incoming = (char *)bip_malloc(CONN_BUFFER_SIZE); incoming = (char *)bip_malloc(CONN_BUFFER_SIZE);
outgoing = list_new(NULL); outgoing = list_new(NULL);

View File

@ -75,9 +75,7 @@ int irc_cli_bip(bip_t *bip, struct link_client *ic, struct line *line);
struct channel *channel_new(const char *name) struct channel *channel_new(const char *name)
{ {
struct channel *chan; struct channel *chan;
chan = calloc(sizeof(struct channel), 1); chan = bip_calloc(sizeof(struct channel), 1);
if (!chan)
fatal("calloc");
chan->name = strdup(name); chan->name = strdup(name);
hash_init(&chan->nicks, HASH_NOCASE); hash_init(&chan->nicks, HASH_NOCASE);
return chan; return chan;
@ -351,10 +349,6 @@ void rotate_who_client(struct link *link)
} }
} }
/*
* parses:join part mode kick kill privmsg quit nick names
* returns: -1 invalid protocol
*/
int irc_dispatch_server(bip_t *bip, struct link_server *server, int irc_dispatch_server(bip_t *bip, struct link_server *server,
struct line *line) struct line *line)
{ {
@ -584,10 +578,8 @@ static void bind_to_link(struct link *l, struct link_client *ic)
LINK(ic) = l; LINK(ic) = l;
l->l_clientc++; l->l_clientc++;
l->l_clientv = realloc(l->l_clientv, l->l_clientc * l->l_clientv = bip_realloc(l->l_clientv, l->l_clientc *
sizeof(struct link_client *)); sizeof(struct link_client *));
if (!l->l_clientv)
fatal("realloc");
l->l_clientv[i] = ic; l->l_clientv[i] = ic;
} }
@ -612,14 +604,12 @@ void unbind_from_link(struct link_client *ic)
l->l_clientv[i - 1] = l->l_clientv[i]; l->l_clientv[i - 1] = l->l_clientv[i];
l->l_clientc--; l->l_clientc--;
l->l_clientv = realloc(l->l_clientv, l->l_clientc * l->l_clientv = bip_realloc(l->l_clientv, l->l_clientc *
sizeof(struct link_client *)); sizeof(struct link_client *));
if (l->l_clientc == 0) { /* realloc was equiv to free() */ if (l->l_clientc == 0) { /* bip_realloc was equiv to free() */
l->l_clientv = NULL; l->l_clientv = NULL;
return; return;
} }
if (!l->l_clientv)
fatal("realloc");
} }
int irc_cli_bip(bip_t *bip, struct link_client *ic, struct line *line) int irc_cli_bip(bip_t *bip, struct link_client *ic, struct line *line)
@ -1272,9 +1262,7 @@ static int irc_join(struct link_server *server, struct line *line)
return ERR_PROTOCOL; return ERR_PROTOCOL;
s_nick = nick_from_ircmask(line->origin); s_nick = nick_from_ircmask(line->origin);
nick = calloc(sizeof(struct nick), 1); nick = bip_calloc(sizeof(struct nick), 1);
if (!nick)
fatal("calloc");
nick->name = s_nick; /* not freeing s_nick */ nick->name = s_nick; /* not freeing s_nick */
hash_insert(&channel->nicks, s_nick, nick); hash_insert(&channel->nicks, s_nick, nick);
return OK_COPY; return OK_COPY;
@ -1529,7 +1517,7 @@ static void mode_add_letter_uniq(struct link_server *s, char c)
if (s->user_mode[i] == c) if (s->user_mode[i] == c)
return; return;
} }
s->user_mode = realloc(s->user_mode, s->user_mode_len + 1); s->user_mode = bip_realloc(s->user_mode, s->user_mode_len + 1);
s->user_mode[s->user_mode_len++] = c; s->user_mode[s->user_mode_len++] = c;
} }
@ -1541,7 +1529,8 @@ static void mode_remove_letter(struct link_server *s, char c)
for (; i < s->user_mode_len - 1; i++) for (; i < s->user_mode_len - 1; i++)
s->user_mode[i] = s->user_mode[i + 1]; s->user_mode[i] = s->user_mode[i + 1];
s->user_mode_len--; s->user_mode_len--;
s->user_mode = realloc(s->user_mode, s->user_mode_len); s->user_mode = bip_realloc(s->user_mode,
s->user_mode_len);
return; return;
} }
} }
@ -1935,7 +1924,7 @@ static struct link_client *irc_accept_new(connection_t *conn)
if (!newconn) if (!newconn)
return NULL; return NULL;
ircc = calloc(sizeof(struct link_client), 1); ircc = bip_calloc(sizeof(struct link_client), 1);
CONN(ircc) = newconn; CONN(ircc) = newconn;
TYPE(ircc) = IRC_TYPE_LOGING_CLIENT; TYPE(ircc) = IRC_TYPE_LOGING_CLIENT;
CONN(ircc)->user_data = ircc; CONN(ircc)->user_data = ircc;
@ -2037,7 +2026,7 @@ struct link_client *irc_client_new(void)
{ {
struct link_client *c; struct link_client *c;
c = calloc(sizeof(struct link_client), 1); c = bip_calloc(sizeof(struct link_client), 1);
list_init(&c->who_queue, list_ptr_cmp); list_init(&c->who_queue, list_ptr_cmp);
return c; return c;
@ -2047,9 +2036,7 @@ struct link_server *irc_server_new(struct link *link, connection_t *conn)
{ {
struct link_server *s; struct link_server *s;
s = calloc(sizeof(struct link_server), 1); s = bip_calloc(sizeof(struct link_server), 1);
if (!s)
fatal("calloc");
TYPE(s) = IRC_TYPE_SERVER; TYPE(s) = IRC_TYPE_SERVER;
hash_init(&s->channels, HASH_NOCASE); hash_init(&s->channels, HASH_NOCASE);
@ -2499,9 +2486,7 @@ void irc_client_free(struct link_client *cli)
struct link *irc_link_new() struct link *irc_link_new()
{ {
struct link *link; struct link *link;
link = calloc(sizeof(struct link), 1); link = bip_calloc(sizeof(struct link), 1);
if (!link)
fatal("calloc");
link->l_server = NULL; link->l_server = NULL;
hash_init(&link->chan_infos, HASH_NOCASE); hash_init(&link->chan_infos, HASH_NOCASE);

View File

@ -34,7 +34,7 @@ struct server {
unsigned short port; unsigned short port;
}; };
#define server_new() calloc(sizeof(struct server), 1) #define server_new() bip_calloc(sizeof(struct server), 1)
#define NICKOP 1 #define NICKOP 1
#define NICKHALFOP (1<<1) #define NICKHALFOP (1<<1)
@ -216,7 +216,7 @@ struct chan_info {
int backlog; int backlog;
}; };
#define chan_info_new() calloc(sizeof(struct chan_info), 1) #define chan_info_new() bip_calloc(sizeof(struct chan_info), 1)
struct link_server { struct link_server {
struct link_connection _link_c; struct link_connection _link_c;

View File

@ -61,9 +61,7 @@ struct line *irc_line_dup(struct line *line)
void _irc_line_append(struct line *l, char *s) void _irc_line_append(struct line *l, char *s)
{ {
l->elemc++; l->elemc++;
l->elemv = realloc(l->elemv, l->elemc * sizeof(char *)); l->elemv = bip_realloc(l->elemv, l->elemc * sizeof(char *));
if (!l)
fatal("realloc");
l->elemv[l->elemc - 1] = s; l->elemv[l->elemc - 1] = s;
} }
@ -114,9 +112,7 @@ struct line *irc_line(char *str)
size_t len; size_t len;
int curelem = 0; int curelem = 0;
line = calloc(sizeof(struct line), 1); line = bip_calloc(sizeof(struct line), 1);
if (!line)
fatal("calloc");
if (str[0] == ':') { if (str[0] == ':') {
space = str + 1; space = str + 1;
@ -138,10 +134,8 @@ struct line *irc_line(char *str)
char *tmp; char *tmp;
line->elemc++; line->elemc++;
line->elemv = realloc(line->elemv, line->elemv = bip_realloc(line->elemv,
line->elemc * sizeof(char *)); line->elemc * sizeof(char *));
if (!line->elemv)
fatal("realloc");
space = str; space = str;
if (*space == ':') { if (*space == ':') {

View File

@ -247,9 +247,7 @@ static int log_add_file(log_t *logdata, char *destination, char *filename)
lfg = hash_get(&logdata->logfgs, destination); lfg = hash_get(&logdata->logfgs, destination);
if (!lfg) { if (!lfg) {
lfg = calloc(sizeof(logfilegroup_t), 1); lfg = bip_calloc(sizeof(logfilegroup_t), 1);
if (!lfg)
fatal("out of memory");
list_init(&lfg->file_group, NULL); list_init(&lfg->file_group, NULL);
lfg->name = strdup(destination); lfg->name = strdup(destination);
if (!lfg->name) if (!lfg->name)
@ -1174,9 +1172,7 @@ log_t *log_new(struct user *user, char *network)
{ {
log_t *logdata; log_t *logdata;
logdata = (log_t*)calloc(sizeof(log_t), 1); logdata = (log_t *)bip_calloc(sizeof(log_t), 1);
if (!logdata)
fatal("out of memory");
logdata->user = user; logdata->user = user;
logdata->network = strdup(network); logdata->network = strdup(network);
hash_init(&logdata->logfgs, HASH_NOCASE); hash_init(&logdata->logfgs, HASH_NOCASE);

View File

@ -348,29 +348,6 @@ int main( int argc, char *argv[] )
#include <stdlib.h> #include <stdlib.h>
/*
unsigned char *md5dup(unsigned char *data, size_t len)
{
md5_context ctx;
unsigned char *md5 = malloc(16);
md5_starts(&ctx);
md5_update(&ctx, data, len);
md5_finish(&ctx, md5);
return md5;
}
unsigned char *strmd5(char *str)
{
size_t length;
if (!str)
return NULL;
length = strlen(str);
if (length == 0)
return NULL;
return md5dup((unsigned char*)str, length);
}
*/
unsigned char *chash_double(char *str, unsigned int seed) unsigned char *chash_double(char *str, unsigned int seed)
{ {
size_t length; size_t length;

View File

@ -39,6 +39,26 @@ void *bip_malloc(size_t size)
return r; return r;
} }
void *bip_calloc(size_t nmemb, size_t size)
{
void *r = calloc(nmemb, size);
if (!r) {
fprintf(conf_global_log_file, 1, strlen("calloc"), "calloc");
exit(28);
}
return r;
}
void *bip_realloc(void *ptr, size_t size)
{
void *r = realloc(ptr, size);
if (size > 0 && ptr == NULL) {
fprintf(conf_global_log_file, 1, strlen("realloc"), "realloc");
exit(28);
}
return r;
}
/* /*
* <nick> ::= <letter> { <letter> | <number> | <special> } * <nick> ::= <letter> { <letter> | <number> | <special> }
* <special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}' * <special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}'

View File

@ -124,5 +124,7 @@ char *checkmode2text(int v);
#endif #endif
#define bool2text(v) ((v) ? "true" : "false") #define bool2text(v) ((v) ? "true" : "false")
void *bip_malloc(size_t size); void *bip_malloc(size_t size);
void *bip_calloc(size_t nmemb, size_t size);
void *bip_realloc(void *ptr, size_t size);
#endif #endif