From e5090a4d86ba3c43c0c0773c71fee92c265ba0ea Mon Sep 17 00:00:00 2001 From: Neil Horman Date: Thu, 3 Aug 2017 08:43:24 -0400 Subject: [PATCH] Fix some string sizing issues My last commit missed a few errors in the size computation of strings, as well as some subtle dereferncing errors Signed-off-by: Neil Horman --- irqbalance.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/irqbalance.c b/irqbalance.c index a8fad1d..0854a3b 100644 --- a/irqbalance.c +++ b/irqbalance.c @@ -320,8 +320,8 @@ gboolean scan(gpointer data) void get_irq_data(struct irq_info *irq, void *data) { char **irqdata = (char **)data; - if (!irqdata) - *irqdata = malloc(24 + 1 + 11 + 20 + 20 + 11); + if (!*irqdata) + *irqdata = calloc(24 + 1 + 11 + 20 + 20 + 11, 1); else *irqdata = realloc(*irqdata, strlen(*irqdata) + 24 + 1 + 11 + 20 + 20 + 11); @@ -334,11 +334,13 @@ void get_object_stat(struct topo_obj *object, void *data) { char **stats = (char **)data; char *irq_data = NULL; + size_t irqdlen; if (g_list_length(object->interrupts) > 0) { for_each_irq(object->interrupts, get_irq_data, &irq_data); } + irqdlen = irq_data ? strlen(irq_data) : 0; /* * Note, the size in both conditional branches below is made up as follows: * strlen(irq_data) - self explanitory @@ -349,14 +351,14 @@ void get_object_stat(struct topo_obj *object, void *data) * This should be adjusted if the string in the sprintf is changed */ if (!*stats) { - *stats = malloc(strlen(irq_data) + 31 + 11 + 20 + 11 + 1); + *stats = calloc(irqdlen + 31 + 11 + 20 + 11 + 1, 1); } else { - *stats = realloc(stats, strlen(*stats) + strlen(irq_data) + 31 + 11 + 20 + 11 + 1); + *stats = realloc(*stats, strlen(*stats) + irqdlen + 31 + 11 + 20 + 11 + 1); } sprintf(*stats + strlen(*stats), "TYPE %d NUMBER %d LOAD %lu SAVE_MODE %d %s", object->obj_type, object->number, object->load, - object->powersave_mode, irq_data); + object->powersave_mode, irq_data ? irq_data : ""); free(irq_data); if (object->obj_type != OBJ_TYPE_CPU) { for_each_object(object->children, get_object_stat, data); @@ -451,15 +453,14 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri } if (!strncmp(buff, "setup", strlen("setup"))) { char banned[512]; - char *setup = malloc(strlen("SLEEP ") + 11 +1); + char *setup = calloc(strlen("SLEEP ") + 11 +1, 1); snprintf(setup, 2048, "SLEEP %d ", sleep_interval); if(g_list_length(cl_banned_irqs) > 0) { for_each_irq(cl_banned_irqs, get_irq_data, setup); } - cpumask_scnprintf(banned, 512, banned_cpus); - setup = realloc(setup, strlen(setup) + strlen(banned) + 1); - snprintf(setup + strlen(setup), strlen(banned), + setup = realloc(setup, strlen(setup) + strlen(banned) + 7 + 1); + snprintf(setup + strlen(setup), strlen(banned) + 7 + 1, "BANNED %s", banned); send(sock, setup, strlen(setup), 0); }