[commands] do not cast return of calloc and allocate struct command on

stack if executed in sync
This commit is contained in:
chme 2016-05-21 05:59:45 +02:00
parent 3c9a7d9119
commit 6112c38220
1 changed files with 14 additions and 17 deletions

View File

@ -131,7 +131,7 @@ commands_base_new(struct event_base *evbase)
struct commands_base *cmdbase; struct commands_base *cmdbase;
int ret; int ret;
cmdbase = (struct commands_base*) calloc(1, sizeof(struct commands_base)); cmdbase = calloc(1, sizeof(struct commands_base));
if (!cmdbase) if (!cmdbase)
{ {
DPRINTF(E_LOG, L_MAIN, "Out of memory for cmdbase\n"); DPRINTF(E_LOG, L_MAIN, "Out of memory for cmdbase\n");
@ -225,32 +225,29 @@ commands_exec_end(struct commands_base *cmdbase, int retvalue)
int int
commands_exec_sync(struct commands_base *cmdbase, command_function func, command_function func_bh, void *arg) commands_exec_sync(struct commands_base *cmdbase, command_function func, command_function func_bh, void *arg)
{ {
struct command *cmd; struct command cmd;
int ret; int ret;
cmd = (struct command*) calloc(1, sizeof(struct command)); memset(&cmd, 0, sizeof(struct command));
cmd->func = func; cmd.func = func;
cmd->func_bh = func_bh; cmd.func_bh = func_bh;
cmd->arg = arg; cmd.arg = arg;
cmd->nonblock = 0; cmd.nonblock = 0;
pthread_mutex_lock(&cmd->lck); pthread_mutex_lock(&cmd.lck);
ret = send_command(cmdbase, cmd); ret = send_command(cmdbase, &cmd);
if (ret < 0) if (ret < 0)
{ {
DPRINTF(E_LOG, L_MAIN, "Error sending command\n"); DPRINTF(E_LOG, L_MAIN, "Error sending command\n");
pthread_mutex_unlock(&cmd->lck); pthread_mutex_unlock(&cmd.lck);
return -1; return -1;
} }
pthread_cond_wait(&cmd->cond, &cmd->lck); pthread_cond_wait(&cmd.cond, &cmd.lck);
pthread_mutex_unlock(&cmd->lck); pthread_mutex_unlock(&cmd.lck);
ret = cmd->ret; return cmd.ret;
free(cmd);
return ret;
} }
int int
@ -259,7 +256,7 @@ commands_exec_async(struct commands_base *cmdbase, command_function func, void *
struct command *cmd; struct command *cmd;
int ret; int ret;
cmd = (struct command*) calloc(1, sizeof(struct command)); cmd = calloc(1, sizeof(struct command));
cmd->func = func; cmd->func = func;
cmd->func_bh = NULL; cmd->func_bh = NULL;
cmd->arg = arg; cmd->arg = arg;