From c71214cefe831729464c7fae26521b6f156c861f Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Tue, 2 Feb 2021 18:37:26 +0100 Subject: [PATCH] Rename nvplist functions to match the rest, fix resource leak, and move the functions to list.c where they belong. nvplist_add() -> add_nvplist() nvplist_add_fmt() -> add_fmt_nvplist() (new) -> free_nvplist --- include/h.h | 5 +++-- src/list.c | 35 +++++++++++++++++++++++++++++++++++ src/misc.c | 24 ------------------------ src/modules/reputation.c | 2 +- src/user.c | 12 ++++++++---- 5 files changed, 47 insertions(+), 31 deletions(-) diff --git a/include/h.h b/include/h.h index a50676520..210710cf2 100644 --- a/include/h.h +++ b/include/h.h @@ -999,6 +999,7 @@ extern void free_security_group(SecurityGroup *s); extern void set_security_group_defaults(void); extern int user_allowed_by_security_group(Client *client, SecurityGroup *s); extern int user_allowed_by_security_group_name(Client *client, char *secgroupname); -extern void nvplist_add(NameValuePrioList **lst, int priority, char *name, char *value); -extern void nvplist_add_fmt(NameValuePrioList **lst, int priority, char *name, FORMAT_STRING(const char *format), ...) __attribute__((format(printf,4,5))); +extern void add_nvplist(NameValuePrioList **lst, int priority, char *name, char *value); +extern void add_fmt_nvplist(NameValuePrioList **lst, int priority, char *name, FORMAT_STRING(const char *format), ...) __attribute__((format(printf,4,5))); +extern void free_nvplist(NameValuePrioList *lst); extern char *get_connect_extinfo(Client *client); diff --git a/src/list.c b/src/list.c index 0ab38d9f5..d08dd8f67 100644 --- a/src/list.c +++ b/src/list.c @@ -567,3 +567,38 @@ NameList *find_name_list_match(NameList *list, char *name) return NULL; } +void add_nvplist(NameValuePrioList **lst, int priority, char *name, char *value) +{ + va_list vl; + NameValuePrioList *e = safe_alloc(sizeof(NameValuePrioList)); + safe_strdup(e->name, name); + if (value && *value) + safe_strdup(e->value, value); + AddListItemPrio(e, *lst, priority); +} + +void add_fmt_nvplist(NameValuePrioList **lst, int priority, char *name, FORMAT_STRING(const char *format), ...) +{ + char value[512]; + va_list vl; + *value = '\0'; + if (format) + { + va_start(vl, format); + vsnprintf(value, sizeof(value), format, vl); + va_end(vl); + } + add_nvplist(lst, priority, name, value); +} + +void free_nvplist(NameValuePrioList *lst) +{ + NameValuePrioList *e, *e_next; + for (e = lst; e; e = e_next) + { + e_next = e->next; + safe_free(e->name); + safe_free(e->value); + safe_free(e); + } +} diff --git a/src/misc.c b/src/misc.c index 4779f9467..f987ba413 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1964,27 +1964,3 @@ char *sendtype_to_cmd(SendType sendtype) return "TAGMSG"; return NULL; } - -void nvplist_add(NameValuePrioList **lst, int priority, char *name, char *value) -{ - va_list vl; - NameValuePrioList *e = safe_alloc(sizeof(NameValuePrioList)); - safe_strdup(e->name, name); - if (value && *value) - safe_strdup(e->value, value); - AddListItemPrio(e, *lst, priority); -} - -void nvplist_add_fmt(NameValuePrioList **lst, int priority, char *name, FORMAT_STRING(const char *format), ...) -{ - char value[512]; - va_list vl; - *value = '\0'; - if (format) - { - va_start(vl, format); - vsnprintf(value, sizeof(value), format, vl); - va_end(vl); - } - nvplist_add(lst, priority, name, value); -} diff --git a/src/modules/reputation.c b/src/modules/reputation.c index a98e9ba07..19418a75f 100644 --- a/src/modules/reputation.c +++ b/src/modules/reputation.c @@ -673,7 +673,7 @@ CMD_FUNC(reputationunperm) int reputation_connect_extinfo(Client *client, NameValuePrioList **list) { - nvplist_add_fmt(list, 0, "reputation", "%d", GetReputation(client)); + add_fmt_nvplist(list, 0, "reputation", "%d", GetReputation(client)); return 0; } diff --git a/src/user.c b/src/user.c index 3c8ad66d2..f809c2517 100644 --- a/src/user.c +++ b/src/user.c @@ -889,17 +889,17 @@ char *get_connect_extinfo(Client *client) /* "class": this should be first */ if (MyUser(client) && client->local->class) - nvplist_add(&list, -100000, "class", client->local->class->name); + add_nvplist(&list, -100000, "class", client->local->class->name); /* "secure": SSL/TLS */ if (MyUser(client) && IsSecure(client)) - nvplist_add(&list, -1000, "secure", tls_get_cipher(client->local->ssl)); + add_nvplist(&list, -1000, "secure", tls_get_cipher(client->local->ssl)); else if (!MyUser(client) && IsSecureConnect(client)) - nvplist_add(&list, -1000, "secure", NULL); + add_nvplist(&list, -1000, "secure", NULL); /* services account? */ if (IsLoggedIn(client)) - nvplist_add(&list, -500, "account", client->user->svid); + add_nvplist(&list, -500, "account", client->user->svid); *retbuf = '\0'; for (e = list; e; e = e->next) @@ -913,5 +913,9 @@ char *get_connect_extinfo(Client *client) /* Cut off last space (unless empty string) */ if (*buf) buf[strlen(buf)-1] = '\0'; + + /* Free the list, as it was only used to build retbuf */ + free_nvplist(list); + return retbuf; }