This should keep Coverity happy.
Signed-off-by: Zane Bitter zbitter@redhat.com --- src/broker/broker.c | 51 ++++++++++++++++++++++++++++++++++---------------- src/lib/host_linux.c | 22 ++++++++++++++++------ 2 files changed, 51 insertions(+), 22 deletions(-)
diff --git a/src/broker/broker.c b/src/broker/broker.c index 63894b7..ead2a61 100644 --- a/src/broker/broker.c +++ b/src/broker/broker.c @@ -45,22 +45,48 @@ uint16_t broker_get_port(void) return port ? port : MATAHARI_PORT; }
+ +static void broker_args_free(char **args) +{ + char **p = args; + + while (*args) { + free(*args++); + } + + free(p); +} + +#define APPEND_ARG(ARGS, COUNT, NEWARG) do { \ + const char *_newarg = strdup(NEWARG); \ + if (((ARGS)[(COUNT)++] = _newarg) == NULL) { \ + mh_err("Failed to allocate string"); \ + broker_args_free(ARGS); \ + return NULL; \ + } \ +} while (0) + static char **broker_args(int argc, char * const argv[]) { int i = 0, o = 0; char portarg[13]; char **newargs = malloc(sizeof(char *) * (argc + 2 + 1));
+ if (!newargs) { + mh_err("Failed to allocate argument list"); + return NULL; + } + if (argc) { - newargs[o++] = strdup(argv[i++]); + APPEND_ARG(newargs, o, argv[i++]); }
snprintf(portarg, sizeof(portarg), "--port=%hu", broker_get_port()); - newargs[o++] = strdup(portarg); - newargs[o++] = strdup("--data-dir=" LOCAL_STATE_DIR "/lib/matahari"); + APPEND_ARG(newargs, o, portarg); + APPEND_ARG(newargs, o, "--data-dir=" LOCAL_STATE_DIR "/lib/matahari");
while (i < argc) { - newargs[o++] = strdup(argv[i++]); + APPEND_ARG(newargs, o, argv[i++]); }
newargs[o] = NULL; @@ -68,24 +94,17 @@ static char **broker_args(int argc, char * const argv[]) return newargs; }
-static void broker_args_free(char **args) -{ - char **p = args; - - while (*args) { - free(*args++); - } - - free(p); -}
int main(int argc, char *argv[]) { + int ret = 1; char **arglist = broker_args(argc, argv);
- int ret = broker_os_start_broker(arglist); + if (arglist) { + ret = broker_os_start_broker(arglist);
- broker_args_free(arglist); + broker_args_free(arglist); + }
return ret; } diff --git a/src/lib/host_linux.c b/src/lib/host_linux.c index c17c7ca..bae10f1 100644 --- a/src/lib/host_linux.c +++ b/src/lib/host_linux.c @@ -39,6 +39,10 @@ #include "matahari/host.h" #include "host_private.h"
+ +#define UUID_STR_BUF_LEN 37 + + const char * host_os_get_cpu_flags(void) { @@ -263,21 +267,25 @@ char *host_os_reboot_uuid(void) const char *file = "/var/run/matahari-reboot-id"; char *uuid = mh_file_first_line(file);
- if(uuid == NULL) { + if (uuid == NULL) { uuid_t buffer; GError* error = NULL;
- uuid = malloc(38); + uuid = malloc(UUID_STR_BUF_LEN); + if (!uuid) { + return NULL; + } + uuid_generate(buffer); uuid_unparse(buffer, uuid);
- if(g_file_set_contents(file, uuid, strlen(uuid), &error) == FALSE) { + if (g_file_set_contents(file, uuid, strlen(uuid), &error) == FALSE) { mh_info("%s", error->message); free(uuid); uuid = strdup(error->message); }
- if(error) { + if (error) { g_error_free(error); } } @@ -291,8 +299,10 @@ const char *host_os_agent_uuid(void) static char *agent_uuid = NULL; uuid_generate(buffer);
- agent_uuid = malloc(38); - uuid_unparse(buffer, agent_uuid); + agent_uuid = malloc(UUID_STR_BUF_LEN); + if (agent_uuid) { + uuid_unparse(buffer, agent_uuid); + }
return agent_uuid; }