Use hub_malloc(), hub_free() etc.

This commit is contained in:
Blair Bonnett 2012-08-17 22:59:59 +12:00
parent 20173e580d
commit 839309a4a8
5 changed files with 30 additions and 32 deletions

View File

@ -485,7 +485,7 @@ static int command_broadcast(struct command_base* cbase, struct hub_user* user,
cbuf_append_format(buf, "*** %s: Delivered to " PRINTF_SIZE_T " user%s", cmd->prefix, recipients, (recipients != 1 ? "s" : "")); cbuf_append_format(buf, "*** %s: Delivered to " PRINTF_SIZE_T " user%s", cmd->prefix, recipients, (recipients != 1 ? "s" : ""));
send_message(cbase, user, buf); send_message(cbase, user, buf);
free(message); hub_free(message);
return 0; return 0;
} }

View File

@ -259,11 +259,11 @@ static struct linked_list* cbfunc_get_user_list(struct plugin_handle* plugin, en
* data in case the user disconnects before the plugin uses the * data in case the user disconnects before the plugin uses the
* list. This way, any hub functions the plugin tries to call will * list. This way, any hub functions the plugin tries to call will
* fail, but at least it won't be trying to access free'd memory. */ * fail, but at least it won't be trying to access free'd memory. */
struct plugin_user* puser = (struct plugin_user*)malloc(sizeof(struct plugin_user)); struct plugin_user* puser = (struct plugin_user*)hub_malloc(sizeof(struct plugin_user));
if(puser == NULL) if(puser == NULL)
{ {
plugin->error_msg = "Unable to allocate memory for list entry in get_user_list."; plugin->error_msg = "Unable to allocate memory for list entry in get_user_list.";
list_clear(new_list, &free); list_clear(new_list, &hub_free);
list_destroy(new_list); list_destroy(new_list);
return NULL; return NULL;
} }
@ -286,7 +286,7 @@ static void cbfunc_free_user_list(struct plugin_handle* handle, struct linked_li
{ {
if(list != NULL) if(list != NULL)
{ {
list_clear(list, &free); list_clear(list, &hub_free);
list_destroy(list); list_destroy(list);
} }
} }

View File

@ -38,17 +38,14 @@ int ucmd_expand_tt(struct plugin_ucmd* ucmd, size_t size)
/* Try to allocate the space. NB we add one to the space to enforce a null /* Try to allocate the space. NB we add one to the space to enforce a null
* byte. */ * byte. */
char* newtt = (char*)malloc(size+1); char* newtt = (char*)hub_malloc_zero(size+1);
if(newtt == NULL) return 0; if(newtt == NULL) return 0;
/* Empty the contents. */
memset(newtt, 0, size+1);
/* Copy any existing data. */ /* Copy any existing data. */
if(ucmd->tt != NULL) if(ucmd->tt != NULL)
{ {
memcpy(newtt, ucmd->tt, ucmd->length); memcpy(newtt, ucmd->tt, ucmd->length);
free(ucmd->tt); hub_free(ucmd->tt);
} }
/* Update the structure. */ /* Update the structure. */
@ -97,7 +94,7 @@ char* ucmd_msg_escape(const char* message)
{ {
/* Allocate the memory we need. */ /* Allocate the memory we need. */
size_t esclen = ucmd_msg_escape_length(message); size_t esclen = ucmd_msg_escape_length(message);
char *escaped = malloc(esclen + 1); char *escaped = hub_malloc(esclen + 1);
int insub = 0; int insub = 0;
size_t i; size_t i;
@ -167,7 +164,7 @@ struct plugin_ucmd* cbfunc_ucmd_create(struct plugin_handle* plugin, const char*
} }
/* Allocate space for the command structure. */ /* Allocate space for the command structure. */
struct plugin_ucmd* cmd = (struct plugin_ucmd*)malloc(sizeof(struct plugin_ucmd)); struct plugin_ucmd* cmd = (struct plugin_ucmd*)hub_malloc(sizeof(struct plugin_ucmd));
if(cmd == NULL) if(cmd == NULL)
{ {
plugin->error_msg = "Not enough memory to create user command."; plugin->error_msg = "Not enough memory to create user command.";
@ -208,7 +205,7 @@ int cbfunc_ucmd_add_chat(struct plugin_handle* plugin, struct plugin_ucmd* ucmd,
* the UCMD escape is needed to handle substitution blocks correctly. */ * the UCMD escape is needed to handle substitution blocks correctly. */
char* temp = ucmd_msg_escape(message); char* temp = ucmd_msg_escape(message);
char* escmsg = adc_msg_escape(temp); char* escmsg = adc_msg_escape(temp);
free(temp); hub_free(temp);
size_t msglen = strlen(escmsg); size_t msglen = strlen(escmsg);
/* Format of a chat message: "BMSG\s%[mySID]\s<double-escaped message>\n". /* Format of a chat message: "BMSG\s%[mySID]\s<double-escaped message>\n".
@ -219,7 +216,7 @@ int cbfunc_ucmd_add_chat(struct plugin_handle* plugin, struct plugin_ucmd* ucmd,
if(ucmd_expand_tt(ucmd, ucmd->capacity + required) == 0) if(ucmd_expand_tt(ucmd, ucmd->capacity + required) == 0)
{ {
plugin->error_msg = "Could not expand memory to store chat message."; plugin->error_msg = "Could not expand memory to store chat message.";
free(escmsg); hub_free(escmsg);
return 0; return 0;
} }
} }
@ -231,7 +228,7 @@ int cbfunc_ucmd_add_chat(struct plugin_handle* plugin, struct plugin_ucmd* ucmd,
/* Copy the message. */ /* Copy the message. */
strncpy(ucmd->tt + ucmd->length, escmsg, msglen); strncpy(ucmd->tt + ucmd->length, escmsg, msglen);
ucmd->length += msglen; ucmd->length += msglen;
free(escmsg); hub_free(escmsg);
/* If it is a 'me' message, add the flag. */ /* If it is a 'me' message, add the flag. */
if(me) if(me)
@ -256,7 +253,7 @@ int cbfunc_ucmd_add_pm(struct plugin_handle* plugin, struct plugin_ucmd* ucmd, c
* the UCMD escape is needed to handle substitution blocks correctly. */ * the UCMD escape is needed to handle substitution blocks correctly. */
char* temp = ucmd_msg_escape(message); char* temp = ucmd_msg_escape(message);
char* escmsg = adc_msg_escape(temp); char* escmsg = adc_msg_escape(temp);
free(temp); hub_free(temp);
size_t msglen = strlen(escmsg); size_t msglen = strlen(escmsg);
/* If no target SID is given, use the keyword expansion %[userSID] for the /* If no target SID is given, use the keyword expansion %[userSID] for the
@ -271,7 +268,7 @@ int cbfunc_ucmd_add_pm(struct plugin_handle* plugin, struct plugin_ucmd* ucmd, c
if(ucmd_expand_tt(ucmd, ucmd->capacity + required) == 0) if(ucmd_expand_tt(ucmd, ucmd->capacity + required) == 0)
{ {
plugin->error_msg = "Could not expand memory to store private message."; plugin->error_msg = "Could not expand memory to store private message.";
free(escmsg); hub_free(escmsg);
return 0; return 0;
} }
} }
@ -293,7 +290,7 @@ int cbfunc_ucmd_add_pm(struct plugin_handle* plugin, struct plugin_ucmd* ucmd, c
/* Message. */ /* Message. */
strncpy(ucmd->tt + ucmd->length, escmsg, msglen); strncpy(ucmd->tt + ucmd->length, escmsg, msglen);
ucmd->length += msglen; ucmd->length += msglen;
free(escmsg); hub_free(escmsg);
/* Add the PM flag and final line break. */ /* Add the PM flag and final line break. */
strncpy(ucmd->tt + ucmd->length, "\\sPM%[mySID]\\n", 14); strncpy(ucmd->tt + ucmd->length, "\\sPM%[mySID]\\n", 14);
@ -378,13 +375,13 @@ int cbfunc_ucmd_send(struct plugin_handle* plugin, struct plugin_user* user, str
void cbfunc_ucmd_free(struct plugin_handle* plugin, struct plugin_ucmd* ucmd){ void cbfunc_ucmd_free(struct plugin_handle* plugin, struct plugin_ucmd* ucmd){
if(ucmd->name != NULL) if(ucmd->name != NULL)
{ {
free(ucmd->name); hub_free(ucmd->name);
ucmd->name = NULL; ucmd->name = NULL;
} }
if(ucmd->tt != NULL) if(ucmd->tt != NULL)
{ {
free(ucmd->tt); hub_free(ucmd->tt);
ucmd->tt = NULL; ucmd->tt = NULL;
} }
free(ucmd); hub_free(ucmd);
} }

View File

@ -86,7 +86,7 @@ int add_reserved_sid(char* nick, int splitcount, void* data)
} }
/* Try to create a structure for the new reserved SID. */ /* Try to create a structure for the new reserved SID. */
struct reserved_sid* newresv = (struct reserved_sid*)malloc(sizeof(struct reserved_sid)); struct reserved_sid* newresv = (struct reserved_sid*)hub_malloc(sizeof(struct reserved_sid));
if(newresv == NULL) if(newresv == NULL)
{ {
LOG_ERROR("Could not allocate memory for reserved SID for %s", nick); LOG_ERROR("Could not allocate memory for reserved SID for %s", nick);
@ -94,11 +94,11 @@ int add_reserved_sid(char* nick, int splitcount, void* data)
} }
/* Try to create a dummy user for the reserved SID. */ /* Try to create a dummy user for the reserved SID. */
newresv->dummy_user = (struct hub_user*)malloc(sizeof(struct hub_user)); newresv->dummy_user = (struct hub_user*)hub_malloc(sizeof(struct hub_user));
if(newresv->dummy_user == NULL) if(newresv->dummy_user == NULL)
{ {
LOG_ERROR("Could not allocate memory for reserved SID for %s", nick); LOG_ERROR("Could not allocate memory for reserved SID for %s", nick);
free(newresv); hub_free(newresv);
return -1; return -1;
} }
strncpy(newresv->dummy_user->id.nick, nick, nicklen+1); strncpy(newresv->dummy_user->id.nick, nick, nicklen+1);
@ -124,8 +124,8 @@ void remove_reserved_sid(void *node)
struct reserved_sid* resv = (struct reserved_sid*)node; struct reserved_sid* resv = (struct reserved_sid*)node;
LOG_INFO("Removing reserved SID %s for %s", sid_to_string(resv->sid), resv->dummy_user->id.nick); LOG_INFO("Removing reserved SID %s for %s", sid_to_string(resv->sid), resv->dummy_user->id.nick);
sid_free(resv->pool, resv->sid); sid_free(resv->pool, resv->sid);
free(resv->dummy_user); hub_free(resv->dummy_user);
free(resv); hub_free(resv);
} }
int uman_init(struct hub_info* hub) int uman_init(struct hub_info* hub)

View File

@ -22,6 +22,7 @@
#include "plugin_api/handle.h" #include "plugin_api/handle.h"
#include "plugin_api/types.h" #include "plugin_api/types.h"
#include "util/list.h" #include "util/list.h"
#include "util/memory.h"
#include "util/misc.h" #include "util/misc.h"
#include "util/config_token.h" #include "util/config_token.h"
@ -107,7 +108,7 @@ void free_data(struct plugin_handle* plugin, struct ucmd_data* data)
} }
/* Done with the data structure. */ /* Done with the data structure. */
free(data); hub_free(data);
} }
} }
@ -242,13 +243,13 @@ int parse_pm(struct parse_state* state, char* args)
return -1; return -1;
} }
args[4] = 0; args[4] = 0;
target = strdup(args); target = hub_strdup(args);
args += 5; args += 5;
} }
/* Add the message. */ /* Add the message. */
int retval = state->plugin->hub.ucmd_add_pm(state->plugin, state->ucmd, target, args, echo); int retval = state->plugin->hub.ucmd_add_pm(state->plugin, state->ucmd, target, args, echo);
if(target != NULL) free(target); if(target != NULL) hub_free(target);
/* Done. */ /* Done. */
if(retval) if(retval)
@ -351,7 +352,7 @@ int parse_line(char *line, int line_number, void* data)
int parse_file(struct plugin_handle* plugin, struct ucmd_data* data, const char* filename) int parse_file(struct plugin_handle* plugin, struct ucmd_data* data, const char* filename)
{ {
/* Create the parser state. */ /* Create the parser state. */
struct parse_state* state = (struct parse_state*)malloc(sizeof(struct parse_state)); struct parse_state* state = (struct parse_state*)hub_malloc(sizeof(struct parse_state));
state->plugin = plugin; state->plugin = plugin;
state->data = data; state->data = data;
state->ucmd = NULL; state->ucmd = NULL;
@ -383,7 +384,7 @@ int parse_file(struct plugin_handle* plugin, struct ucmd_data* data, const char*
/* Clean up memory from the state. If ucmd is not null, then there was an /* Clean up memory from the state. If ucmd is not null, then there was an
* error and it is a partially-processed object we also need to free. */ * error and it is a partially-processed object we also need to free. */
if(state->ucmd != NULL) plugin->hub.ucmd_free(plugin, state->ucmd); if(state->ucmd != NULL) plugin->hub.ucmd_free(plugin, state->ucmd);
free(state); hub_free(state);
/* Done. */ /* Done. */
return retval; return retval;
@ -397,7 +398,7 @@ int parse_config(struct plugin_handle* plugin, const char* config)
int got_file = 0; int got_file = 0;
/* Create space for the data we need. */ /* Create space for the data we need. */
struct ucmd_data *data = (struct ucmd_data *)malloc(sizeof(struct ucmd_data)); struct ucmd_data *data = (struct ucmd_data *)hub_malloc(sizeof(struct ucmd_data));
if(data == NULL){ if(data == NULL){
plugin->error_msg = "Could not allocate data storage."; plugin->error_msg = "Could not allocate data storage.";
return -1; return -1;
@ -414,7 +415,7 @@ int parse_config(struct plugin_handle* plugin, const char* config)
* lists have been initialised. */ * lists have been initialised. */
int j; int j;
for(j = 0; j < i; j++) list_destroy(data->commands[j]); for(j = 0; j < i; j++) list_destroy(data->commands[j]);
free(data); hub_free(data);
plugin->error_msg = "Could not allocate data storage."; plugin->error_msg = "Could not allocate data storage.";
return -1; return -1;
} }