Use glib for all allocations

- Removes need to check for malloc failure
- Removes need for NULL checks on free
- Adds checks for integer overflows
- Removes some extra memset calls
- Removes chance of mixing libc and glib malloc/free
This commit is contained in:
TingPing
2014-12-28 06:37:25 -05:00
parent c4cb1b25ec
commit 83032b1aa3
46 changed files with 796 additions and 1082 deletions

View File

@@ -212,7 +212,7 @@ tcp_send_queue (server *serv)
buf--;
serv->outbound_queue = g_slist_remove (serv->outbound_queue, buf);
free (buf);
g_free (buf);
list = serv->outbound_queue;
} else
{
@@ -234,7 +234,7 @@ tcp_send_len (server *serv, char *buf, int len)
if (!prefs.hex_net_throttle)
return server_send_real (serv, buf, len);
dbuf = malloc (len + 2); /* first byte is the priority */
dbuf = g_malloc (len + 2); /* first byte is the priority */
dbuf[0] = 2; /* pri 2 for most things */
memcpy (dbuf + 1, buf, len);
dbuf[len + 1] = 0;
@@ -527,7 +527,7 @@ server_close_pipe (int *pipefd) /* see comments below */
{
close (pipefd[0]); /* close WRITE end first to cause an EOF on READ */
close (pipefd[1]); /* in giowin32, and end that thread. */
free (pipefd);
g_free (pipefd);
return FALSE;
}
@@ -560,7 +560,7 @@ server_stopconnecting (server * serv)
{
/* if we close the pipe now, giowin32 will crash. */
int *pipefd = malloc (sizeof (int) * 2);
int *pipefd = g_new (int, 2);
pipefd[0] = serv->childwrite;
pipefd[1] = serv->childread;
g_idle_add ((GSourceFunc)server_close_pipe, pipefd);
@@ -1282,7 +1282,7 @@ traverse_socks5 (int print_fd, int sok, char *serverAddr, int port)
addrlen = strlen (serverAddr);
packetlen = 4 + 1 + addrlen + 2;
sc2 = malloc (packetlen);
sc2 = g_malloc (packetlen);
sc2[0] = 5; /* version */
sc2[1] = 1; /* command */
sc2[2] = 0; /* reserved */
@@ -1291,7 +1291,7 @@ traverse_socks5 (int print_fd, int sok, char *serverAddr, int port)
memcpy (sc2 + 5, serverAddr, addrlen);
*((unsigned short *) (sc2 + 5 + addrlen)) = htons (port);
send (sok, sc2, packetlen, 0);
free (sc2);
g_free (sc2);
/* consume all of the reply */
if (recv (sok, buf, 4, 0) != 4)
@@ -1542,7 +1542,7 @@ server_child (server * serv)
if (proxy_type) {
char *c;
c = strchr (proxy, ':') + 3;
proxy_host = strdup (c);
proxy_host = g_strdup (c);
c = strchr (proxy_host, ':');
*c = '\0';
proxy_port = atoi (c + 1);
@@ -1557,7 +1557,7 @@ server_child (server * serv)
prefs.hex_net_proxy_use != 2) /* proxy is NOT dcc-only */
{
proxy_type = prefs.hex_net_proxy_type;
proxy_host = strdup (prefs.hex_net_proxy_host);
proxy_host = g_strdup (prefs.hex_net_proxy_host);
proxy_port = prefs.hex_net_proxy_port;
}
}
@@ -1570,7 +1570,7 @@ server_child (server * serv)
snprintf (buf, sizeof (buf), "9\n%s\n", proxy_host);
write (serv->childwrite, buf, strlen (buf));
ip = net_resolve (ns_server, proxy_host, proxy_port, &real_hostname);
free (proxy_host);
g_free (proxy_host);
if (!ip)
{
write (serv->childwrite, "1\n", 2);
@@ -1589,7 +1589,7 @@ server_child (server * serv)
goto xit;
}
} else /* otherwise we can just use the hostname */
proxy_ip = strdup (hostname);
proxy_ip = g_strdup (hostname);
} else
{
ip = net_resolve (ns_server, hostname, port, &real_hostname);
@@ -1657,12 +1657,9 @@ xit:
/* no need to free ip/real_hostname, this process is exiting */
#ifdef WIN32
/* under win32 we use a thread -> shared memory, must free! */
if (proxy_ip)
free (proxy_ip);
if (ip)
free (ip);
if (real_hostname)
free (real_hostname);
g_free (proxy_ip);
g_free (ip);
g_free (real_hostname);
#endif
return 0;
@@ -1827,7 +1824,7 @@ server_set_encoding (server *serv, char *new_encoding)
if (serv->encoding)
{
free (serv->encoding);
g_free (serv->encoding);
/* can be left as NULL to indicate system encoding */
serv->encoding = NULL;
serv->using_cp1255 = FALSE;
@@ -1836,7 +1833,7 @@ server_set_encoding (server *serv, char *new_encoding)
if (new_encoding)
{
serv->encoding = strdup (new_encoding);
serv->encoding = g_strdup (new_encoding);
/* the serverlist GUI might have added a space
and short description - remove it. */
space = strchr (serv->encoding, ' ');
@@ -1858,8 +1855,7 @@ server_new (void)
static int id = 0;
server *serv;
serv = malloc (sizeof (struct server));
memset (serv, 0, sizeof (struct server));
serv = g_new0 (struct server, 1);
/* use server.c and proto-irc.c functions */
server_fill_her_up (serv);
@@ -1885,19 +1881,15 @@ is_server (server *serv)
void
server_set_defaults (server *serv)
{
if (serv->chantypes)
free (serv->chantypes);
if (serv->chanmodes)
free (serv->chanmodes);
if (serv->nick_prefixes)
free (serv->nick_prefixes);
if (serv->nick_modes)
free (serv->nick_modes);
g_free (serv->chantypes);
g_free (serv->chanmodes);
g_free (serv->nick_prefixes);
g_free (serv->nick_modes);
serv->chantypes = strdup ("#&!+");
serv->chanmodes = strdup ("beI,k,l");
serv->nick_prefixes = strdup ("@%+");
serv->nick_modes = strdup ("ohv");
serv->chantypes = g_strdup ("#&!+");
serv->chanmodes = g_strdup ("beI,k,l");
serv->nick_prefixes = g_strdup ("@%+");
serv->nick_modes = g_strdup ("ohv");
serv->nickcount = 1;
serv->end_of_motd = FALSE;
@@ -2001,9 +1993,8 @@ server_away_free_messages (server *serv)
if (away->server == serv)
{
away_list = g_slist_remove (away_list, away);
if (away->message)
free (away->message);
free (away);
g_free (away->message);
g_free (away);
next = away_list;
}
list = next;
@@ -2017,20 +2008,17 @@ server_away_save_message (server *serv, char *nick, char *msg)
if (away) /* Change message for known user */
{
if (away->message)
free (away->message);
away->message = strdup (msg);
} else
/* Create brand new entry */
g_free (away->message);
away->message = g_strdup (msg);
}
else
{
away = malloc (sizeof (struct away_msg));
if (away)
{
away->server = serv;
safe_strcpy (away->nick, nick, sizeof (away->nick));
away->message = strdup (msg);
away_list = g_slist_prepend (away_list, away);
}
/* Create brand new entry */
away = g_new(struct away_msg, 1);
away->server = serv;
safe_strcpy (away->nick, nick, sizeof (away->nick));
away->message = g_strdup (msg);
away_list = g_slist_prepend (away_list, away);
}
}
@@ -2045,16 +2033,13 @@ server_free (server *serv)
serv->flush_queue (serv);
server_away_free_messages (serv);
free (serv->nick_modes);
free (serv->nick_prefixes);
free (serv->chanmodes);
free (serv->chantypes);
if (serv->bad_nick_prefixes)
free (serv->bad_nick_prefixes);
if (serv->last_away_reason)
free (serv->last_away_reason);
if (serv->encoding)
free (serv->encoding);
g_free (serv->nick_modes);
g_free (serv->nick_prefixes);
g_free (serv->chanmodes);
g_free (serv->chantypes);
g_free (serv->bad_nick_prefixes);
g_free (serv->last_away_reason);
g_free (serv->encoding);
if (serv->favlist)
g_slist_free_full (serv->favlist, (GDestroyNotify) servlist_favchan_free);
#ifdef USE_OPENSSL
@@ -2064,7 +2049,7 @@ server_free (server *serv)
fe_server_callback (serv);
free (serv);
g_free (serv);
notify_cleanup ();
}