From 10ff2d192cd5b3f8268f7cb8e07964c5cf4c4054 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 31 Jul 2016 01:57:04 -0500 Subject: [PATCH] alias: convert to RAII/std::string/etc, no more rb_dictionary use (ref #202) --- include/ircd/parse.h | 3 ++- include/ircd/s_conf.h | 4 ++-- ircd/newconf.cc | 22 ++++++++-------------- ircd/parse.cc | 2 +- ircd/s_conf.cc | 22 +--------------------- modules/m_alias.cc | 21 +++++++++------------ 6 files changed, 23 insertions(+), 51 deletions(-) diff --git a/include/ircd/parse.h b/include/ircd/parse.h index 58c3774e0..4f45b6362 100644 --- a/include/ircd/parse.h +++ b/include/ircd/parse.h @@ -32,6 +32,7 @@ struct Message; struct Client; struct MsgBuf; +struct alias_entry; extern void parse(struct Client *, char *, char *); extern void handle_encap(struct MsgBuf *, struct Client *, struct Client *, @@ -40,7 +41,7 @@ extern void mod_add_cmd(struct Message *msg); extern void mod_del_cmd(struct Message *msg); extern char *reconstruct_parv(int parc, const char *parv[]); -extern rb_dictionary *alias_dict; +extern std::map, case_insensitive_less> alias_dict; extern std::map cmd_dict; #endif /* INCLUDED_parse_h_h */ diff --git a/include/ircd/s_conf.h b/include/ircd/s_conf.h index 8531cbf79..5297f5535 100644 --- a/include/ircd/s_conf.h +++ b/include/ircd/s_conf.h @@ -304,8 +304,8 @@ struct admin_info struct alias_entry { - char *name; - char *target; + std::string name; + std::string target; int flags; /* reserved for later use */ }; diff --git a/ircd/newconf.cc b/ircd/newconf.cc index 36d41e20d..5014ec3f1 100644 --- a/ircd/newconf.cc +++ b/ircd/newconf.cc @@ -52,7 +52,7 @@ static rb_dlink_list yy_shared_list; static rb_dlink_list yy_cluster_list; static struct oper_conf *yy_oper = NULL; -static struct alias_entry *yy_alias = NULL; +static std::shared_ptr yy_alias; static char *yy_blacklist_host = NULL; static char *yy_blacklist_reason = NULL; @@ -1815,10 +1815,10 @@ conf_set_service_name(void *data) static int conf_begin_alias(struct TopConf *tc) { - yy_alias = (alias_entry *)rb_malloc(sizeof(struct alias_entry)); + yy_alias = std::make_shared(); if (conf_cur_block_name != NULL) - yy_alias->name = rb_strdup(conf_cur_block_name); + yy_alias->name = conf_cur_block_name; yy_alias->flags = 0; @@ -1831,25 +1831,19 @@ conf_end_alias(struct TopConf *tc) if (yy_alias == NULL) return -1; - if (yy_alias->name == NULL) + if (yy_alias->name.size() == 0) { conf_report_error("Ignoring alias -- must have a name."); - - rb_free(yy_alias); - return -1; } - if (yy_alias->target == NULL) + if (yy_alias->target.size() == 0) { conf_report_error("Ignoring alias -- must have a target."); - - rb_free(yy_alias); - return -1; } - rb_dictionary_add(alias_dict, yy_alias->name, yy_alias); + alias_dict[yy_alias->name] = std::move(yy_alias); return 0; } @@ -1860,7 +1854,7 @@ conf_set_alias_name(void *data) if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */ return; - yy_alias->name = rb_strdup((const char *)data); + yy_alias->name = (const char *) data; } static void @@ -1869,7 +1863,7 @@ conf_set_alias_target(void *data) if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */ return; - yy_alias->target = rb_strdup((const char *)data); + yy_alias->target = (const char *) data; } static void diff --git a/ircd/parse.cc b/ircd/parse.cc index ad9648911..76d57e2d8 100644 --- a/ircd/parse.cc +++ b/ircd/parse.cc @@ -41,7 +41,7 @@ #include #include -rb_dictionary *alias_dict = NULL; +std::map, case_insensitive_less> alias_dict; std::map cmd_dict; static void cancel_clients(struct Client *, struct Client *); diff --git a/ircd/s_conf.cc b/ircd/s_conf.cc index b82f478ff..180c0fd72 100644 --- a/ircd/s_conf.cc +++ b/ircd/s_conf.cc @@ -810,9 +810,6 @@ set_default_conf(void) ConfigFileEntry.nicklen = NICKLEN; ConfigFileEntry.certfp_method = RB_SSL_CERTFP_METH_CERT_SHA1; ConfigFileEntry.hide_opers_in_whois = 0; - - if (!alias_dict) - alias_dict = rb_dictionary_create("alias", reinterpret_cast(rb_strcasecmp)); } /* @@ -1416,19 +1413,6 @@ read_conf_files(bool cold) fclose(conf_fbfile_in); } -/* - * free an alias{} entry. - */ -static void -free_alias_cb(rb_dictionary_element *ptr, void *unused) -{ - struct alias_entry *aptr = (alias_entry *)ptr->data; - - rb_free(aptr->name); - rb_free(aptr->target); - rb_free(aptr); -} - /* * clear_out_old_conf * @@ -1524,11 +1508,7 @@ clear_out_old_conf(void) } /* remove any aliases... -- nenolod */ - if (alias_dict != NULL) - { - rb_dictionary_destroy(alias_dict, free_alias_cb, NULL); - alias_dict = NULL; - } + alias_dict.clear(); del_blacklist_all(); diff --git a/modules/m_alias.cc b/modules/m_alias.cc index da6c3652b..ef28eada6 100644 --- a/modules/m_alias.cc +++ b/modules/m_alias.cc @@ -53,19 +53,16 @@ static const struct MessageEntry alias_msgtab[] = static inline void create_aliases(void) { - rb_dictionary_iter iter; - s_assert(rb_dlink_list_length(&alias_messages) == 0); - void *elem; - RB_DICTIONARY_FOREACH(elem, &iter, alias_dict) + for (auto it = alias_dict.begin(); it != alias_dict.end(); it++) { - const auto alias(reinterpret_cast(elem)); - struct Message *message = (Message *)rb_malloc(sizeof(*message) + strlen(alias->name) + 1); + std::shared_ptr alias = it->second; + struct Message *message = (Message *)rb_malloc(sizeof(*message) + alias->name.size() + 1); char *cmd = (char*)message + sizeof(*message); /* copy the alias name as it will be freed early on a rehash */ - strcpy(cmd, alias->name); + strcpy(cmd, alias->name.c_str()); message->cmd = cmd; memcpy(message->handlers, alias_msgtab, sizeof(alias_msgtab)); @@ -113,7 +110,7 @@ static void m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, int parc, const char **parv) { struct Client *target_p; - struct alias_entry *aptr = (alias_entry *)rb_dictionary_retrieve(alias_dict, msgbuf->cmd); + std::shared_ptr aptr = alias_dict[msgbuf->cmd]; char *p, *str; if(aptr == NULL) @@ -129,7 +126,7 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, if(!IsFloodDone(client_p) && client_p->localClient->receiveM > 20) flood_endgrace(client_p); - p = strchr(aptr->target, '@'); + p = strchr(aptr->target.c_str(), '@'); if(p != NULL) { /* user@server */ @@ -140,14 +137,14 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, else { /* nick, must be +S */ - target_p = find_named_person(aptr->target); + target_p = find_named_person(aptr->target.c_str()); if(target_p != NULL && !IsService(target_p)) target_p = NULL; } if(target_p == NULL) { - sendto_one_numeric(client_p, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target); + sendto_one_numeric(client_p, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target.c_str()); return; } @@ -160,6 +157,6 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, sendto_one(target_p, ":%s PRIVMSG %s :%s", get_id(client_p, target_p), - p != NULL ? aptr->target : get_id(target_p, target_p), + p != NULL ? aptr->target.c_str() : get_id(target_p, target_p), str); }