From 670e1c5770230517aa0f1c533268448babe20650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Mon, 8 Jan 2018 23:16:14 +0100 Subject: [PATCH] kike: introduce cstr_set() --- kike.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/kike.c b/kike.c index abd0e98..4c97f0e 100644 --- a/kike.c +++ b/kike.c @@ -901,8 +901,7 @@ client_unregister (struct client *c, const char *reason) client_add_to_whowas (c); str_map_set (&c->ctx->users, c->nickname, NULL); - free (c->nickname); - c->nickname = NULL; + cstr_set (&c->nickname, NULL); c->registered = false; } @@ -1442,11 +1441,9 @@ irc_handle_nick (const struct irc_message *msg, struct client *c) // Release the old nickname and allocate a new one if (c->nickname) - { str_map_set (&ctx->users, c->nickname, NULL); - free (c->nickname); - } - c->nickname = xstrdup (nickname); + + cstr_set (&c->nickname, xstrdup (nickname)); str_map_set (&ctx->users, nickname, c); if (!c->registered) @@ -1469,10 +1466,8 @@ irc_handle_user (const struct irc_message *msg, struct client *c) if (!irc_is_valid_user (username)) username = "xxx"; - free (c->username); - c->username = xstrdup (username); - free (c->realname); - c->realname = xstrdup (realname); + cstr_set (&c->username, xstrdup (username)); + cstr_set (&c->realname, xstrdup (realname)); unsigned long m; if (xstrtoul (&m, mode, 10)) @@ -1872,8 +1867,7 @@ mode_processor_do_key (struct mode_processor *self) str_append_c (&self->removed, self->mode_char); strv_append (&self->removed_params, self->channel->key); - free (self->channel->key); - self->channel->key = NULL; + cstr_set (&self->channel->key, NULL); } else if (!irc_is_valid_key (target)) // TODO: we should notify the user somehow @@ -2532,11 +2526,9 @@ irc_handle_topic (const struct irc_message *msg, struct client *c) && !(user->modes & IRC_CHAN_MODE_OPERATOR)) RETURN_WITH_REPLY (c, IRC_ERR_CHANOPRIVSNEEDED, target); - free (chan->topic); - free (chan->topic_who); - chan->topic = xstrdup (msg->params.vector[1]); - chan->topic_who = xstrdup_printf - ("%s!%s@%s", c->nickname, c->username, c->hostname); + cstr_set (&chan->topic, xstrdup (msg->params.vector[1])); + cstr_set (&chan->topic_who, xstrdup_printf + ("%s!%s@%s", c->nickname, c->username, c->hostname)); chan->topic_time = time (NULL); char *message = xstrdup_printf (":%s!%s@%s TOPIC %s :%s", @@ -2800,14 +2792,12 @@ irc_handle_away (const struct irc_message *msg, struct client *c) { if (msg->params.len < 1) { - free (c->away_message); - c->away_message = NULL; + cstr_set (&c->away_message, NULL); irc_send_reply (c, IRC_RPL_UNAWAY); } else { - free (c->away_message); - c->away_message = xstrdup (msg->params.vector[0]); + cstr_set (&c->away_message, xstrdup (msg->params.vector[0])); irc_send_reply (c, IRC_RPL_NOWAWAY); } } @@ -3362,8 +3352,7 @@ on_client_gni_resolved (int result, char *host, char *port, void *user_data) print_debug ("%s: %s", "getnameinfo", gai_strerror (result)); else { - free (c->hostname); - c->hostname = xstrdup (host); + cstr_set (&c->hostname, xstrdup (host)); (void) port; }