@@ -3665,8 +3665,11 @@ static int handle_buffer_option(struct tracecmd_input *handle,
if (!buff->clock)
return -1;
- if (*name == '\0' && !handle->trace_clock)
+ if (*name == '\0' && !handle->trace_clock) {
handle->trace_clock = strdup(buff->clock);
+ if (!handle->trace_clock)
+ return -1;
+ }
if (id == TRACECMD_OPTION_BUFFER) {
if (save_read_number(handle->pevent, data, &size, &rsize, 4, &tmp))
@@ -3855,9 +3858,13 @@ static int handle_options(struct tracecmd_input *handle)
break;
case TRACECMD_OPTION_UNAME:
handle->uname = strdup(buf);
+ if (handle->uname == NULL)
+ return -1;
break;
case TRACECMD_OPTION_VERSION:
handle->version = strdup(buf);
+ if (handle->version == NULL)
+ return -1;
break;
case TRACECMD_OPTION_HOOK:
hook = tracecmd_create_event_hook(buf);
@@ -5967,9 +5974,10 @@ tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx)
new_handle->parent = handle;
new_handle->cpustats = NULL;
new_handle->hooks = NULL;
- if (handle->uname)
+ if (handle->uname) {
/* Ignore if fails to malloc, no biggy */
new_handle->uname = strdup(handle->uname);
+ }
tracecmd_ref(handle);
new_handle->fd = dup(handle->fd);
@@ -1229,6 +1229,8 @@ static int get_trace_req_protos(char *buf, int length,
while (i > 0 && j < (count - 1)) {
i -= strlen(p) + 1;
plist->names[j++] = strdup(p);
+ if (plist->names[j++] == NULL)
+ goto error;
p += strlen(p) + 1;
}
@@ -1593,6 +1595,10 @@ int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle,
*page_size = ntohl(msg.trace_resp.page_size);
*trace_id = ntohll(msg.trace_resp.trace_id);
*tsync_proto = strdup(msg.trace_resp.tsync_proto_name);
+ if (*tsync_proto == NULL) {
+ ret = -ENOMEM;
+ goto out;
+ }
*tsync_port = ntohl(msg.trace_resp.tsync_port);
*ports = calloc(*nr_cpus, sizeof(**ports));
if (!*ports) {
@@ -230,6 +230,7 @@ void tracecmd_set_out_clock(struct tracecmd_output *handle, const char *clock)
if (handle && clock) {
free(handle->trace_clock);
handle->trace_clock = strdup(clock);
+ /* TODO: report error if failed to allocate */
}
}
@@ -882,6 +883,9 @@ static void glob_events(struct tracecmd_output *handle,
for (i = 0; i < globbuf.gl_pathc; i++) {
file = globbuf.gl_pathv[i];
system = strdup(file + events_len + 1);
+ if (system == NULL)
+ /* ?? should we warn? */
+ continue;
system = strtok_r(system, "/", &ptr);
if (!ptr) {
/* ?? should we warn? */
@@ -231,16 +231,22 @@ static int kvm_open_vcpu_dir(struct kvm_clock_sync *kvm, int i, char *dir_str)
snprintf(path, sizeof(path), "%s/%s",
dir_str, entry->d_name);
kvm->clock_files[i].offsets = strdup(path);
+ if (kvm->clock_files[i].offsets == NULL)
+ goto error;
}
if (!strcmp(entry->d_name, KVM_DEBUG_SCALING_FILE)) {
snprintf(path, sizeof(path), "%s/%s",
dir_str, entry->d_name);
kvm->clock_files[i].scalings = strdup(path);
+ if (kvm->clock_files[i].scalings == NULL)
+ goto error;
}
if (!strcmp(entry->d_name, KVM_DEBUG_FRACTION_FILE)) {
snprintf(path, sizeof(path), "%s/%s",
dir_str, entry->d_name);
kvm->clock_files[i].frac = strdup(path);
+ if (kvm->clock_files[i].frac == NULL)
+ goto error;
}
}
}
@@ -764,6 +764,8 @@ tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval,
tsync->trace_id = trace_id;
tsync->loop_interval = loop_interval;
tsync->proto_name = strdup(proto_name);
+ if (tsync->proto_name == NULL)
+ goto error;
tsync->msg_handle = tracecmd_msg_handle_alloc(fd, 0);
if (!tsync->msg_handle) {
@@ -773,8 +775,11 @@ tracecmd_tsync_with_guest(unsigned long long trace_id, int loop_interval,
tsync->guest_pid = guest_pid;
tsync->vcpu_count = guest_cpus;
- if (clock)
+ if (clock) {
tsync->clock_str = strdup(clock);
+ if (tsync->clock_str == NULL)
+ goto error;
+ }
pthread_mutex_init(&tsync->lock, NULL);
pthread_cond_init(&tsync->cond, NULL);
pthread_barrier_init(&tsync->first_sync, NULL, 2);
@@ -964,9 +969,14 @@ tracecmd_tsync_with_host(int fd, const char *proto, const char *clock,
return NULL;
tsync->proto_name = strdup(proto);
+ if (tsync->proto_name == NULL)
+ goto error;
tsync->msg_handle = tracecmd_msg_handle_alloc(fd, 0);
- if (clock)
+ if (clock) {
tsync->clock_str = strdup(clock);
+ if (tsync->clock_str == NULL)
+ goto error;
+ }
tsync->remote_id = remote_id;
tsync->local_id = local_id;
@@ -221,6 +221,8 @@ void tracecmd_parse_ftrace_printk(struct tep_handle *pevent,
addr = strtoull(addr_str, NULL, 16);
/* fmt still has a space, skip it */
printk = strdup(fmt+1);
+ if (printk == NULL)
+ return; /* TODO: return error */
line = strtok_r(NULL, "\n", &next);
tep_register_print_string(pevent, printk, addr);
free(printk);
@@ -255,6 +255,8 @@ static void add_reset_trigger(const char *file)
if (!reset)
die("Failed to allocate reset");
reset->path = strdup(file);
+ if (reset->path == NULL)
+ die("Failed to allocate path");
reset->next = reset_triggers;
reset_triggers = reset;
@@ -371,8 +373,11 @@ struct buffer_instance *allocate_instance(const char *name)
instance = calloc(1, sizeof(*instance));
if (!instance)
return NULL;
- if (name)
+ if (name) {
instance->name = strdup(name);
+ if (instance->name == NULL)
+ goto error;
+ }
if (tracefs_instance_exists(name)) {
instance->tracefs = tracefs_instance_create(name);
if (!instance->tracefs)
@@ -2970,6 +2975,8 @@ create_event(struct buffer_instance *instance, char *path, struct event_list *ol
if (old_event->trigger) {
if (check_file_in_dir(path_dirname, "trigger")) {
event->trigger = strdup(old_event->trigger);
+ if (event->trigger == NULL)
+ die("Failed to allocate trigger");
ret = asprintf(&p, "%s/trigger", path_dirname);
if (ret < 0)
die("Failed to allocate trigger path for %s", path);
@@ -6651,6 +6658,8 @@ static void parse_record_options(int argc,
case OPT_tsoffset:
cmd_check_die(ctx, CMD_set, *(argv+1), "--ts-offset");
ctx->date2ts = strdup(optarg);
+ if (ctx->date2ts == NULL)
+ die("Faield to allocate ts-offset");
if (ctx->data_flags & DATA_FL_DATE)
die("Can not use both --date and --ts-offset");
ctx->data_flags |= DATA_FL_OFFSET;
@@ -6712,6 +6721,8 @@ static void parse_record_options(int argc,
!tracecmd_compress_is_supported(optarg, NULL))
die("Compression algorithm %s is not supported", optarg);
ctx->compression = strdup(optarg);
+ if (ctx->compression == NULL)
+ die("Faield to allocate compression algorithm");
break;
case OPT_file_ver:
if (ctx->curr_cmd != CMD_record && ctx->curr_cmd != CMD_record_agent)
@@ -367,6 +367,8 @@ static double parse_file(struct tracecmd_input *handle,
int fd;
output = strdup(output_file);
+ if (output == NULL)
+ die("Faield to allocate output_file");
dir = dirname(output);
base = basename(output);
@@ -542,8 +544,11 @@ void trace_split (int argc, char **argv)
page_size = tracecmd_page_size(handle);
- if (!output)
+ if (!output) {
output = strdup(input_file);
+ if (output == NULL)
+ die("Failed to allocate output");
+ }
if (!repeat) {
output = realloc(output, strlen(output) + 3);