From 503a3e6e9fc8aa1d7dd9e8ba86fb21edcdcfe502 Mon Sep 17 00:00:00 2001 From: Yunfeng Ye Date: Fri, 11 Oct 2019 23:41:17 +0800 Subject: [PATCH 23/53] correct to use realloc() function The man doc about realloc() say: " If realloc() fails the original block is left untouched; it is not freed or move " So make the handling of realloc() function correctly. In addition, there is another problem about parameter using in sock_handle(), it should be use the address of @setup instead of @setup. Signed-off-by: Yunfeng Ye --- irqbalance.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/irqbalance.c b/irqbalance.c index a06809c..e53bf65 100644 --- a/irqbalance.c +++ b/irqbalance.c @@ -308,14 +308,18 @@ gboolean scan(gpointer data __attribute__((unused))) void get_irq_data(struct irq_info *irq, void *data) { char **irqdata = (char **)data; + char *newptr = NULL; + if (!*irqdata) - *irqdata = calloc(24 + 1 + 11 + 20 + 20 + 11, 1); + newptr = calloc(24 + 1 + 11 + 20 + 20 + 11, 1); else - *irqdata = realloc(*irqdata, strlen(*irqdata) + 24 + 1 + 11 + 20 + 20 + 11); + newptr = realloc(*irqdata, strlen(*irqdata) + 24 + 1 + 11 + 20 + 20 + 11); - if (!*irqdata) + if (!newptr) return; + *irqdata = newptr; + sprintf(*irqdata + strlen(*irqdata), "IRQ %d LOAD %lu DIFF %lu CLASS %d ", irq->irq, irq->load, (irq->irq_count - irq->last_irq_count), irq->class); @@ -325,6 +329,7 @@ void get_object_stat(struct topo_obj *object, void *data) { char **stats = (char **)data; char *irq_data = NULL; + char *newptr = NULL; size_t irqdlen; if (g_list_length(object->interrupts) > 0) { @@ -342,13 +347,17 @@ 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 = calloc(irqdlen + 31 + 11 + 20 + 11 + 1, 1); + newptr = calloc(irqdlen + 31 + 11 + 20 + 11 + 1, 1); } else { - *stats = realloc(*stats, strlen(*stats) + irqdlen + 31 + 11 + 20 + 11 + 1); + newptr = realloc(*stats, strlen(*stats) + irqdlen + 31 + 11 + 20 + 11 + 1); } - if (!*stats) + if (!newptr) { + free(irq_data); return; + } + + *stats = newptr; sprintf(*stats + strlen(*stats), "TYPE %d NUMBER %d LOAD %lu SAVE_MODE %d %s", object->obj_type, object->number, object->load, @@ -465,20 +474,24 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri if (!strncmp(buff, "setup", strlen("setup"))) { char banned[512]; char *setup = calloc(strlen("SLEEP ") + 11 + 1, 1); + char *newptr = NULL; if (!setup) goto out_close; snprintf(setup, strlen("SLEEP ") + 11 + 1, "SLEEP %d ", sleep_interval); if(g_list_length(cl_banned_irqs) > 0) { - for_each_irq(cl_banned_irqs, get_irq_data, setup); + for_each_irq(cl_banned_irqs, get_irq_data, &setup); } cpumask_scnprintf(banned, 512, banned_cpus); - setup = realloc(setup, strlen(setup) + strlen(banned) + 7 + 1); - if (!setup) - goto out_close; + newptr = realloc(setup, strlen(setup) + strlen(banned) + 7 + 1); + if (!newptr) + goto out_free_setup; + + setup = newptr; snprintf(setup + strlen(setup), strlen(banned) + 7 + 1, "BANNED %s", banned); send(sock, setup, strlen(setup), 0); +out_free_setup: free(setup); } -- 2.23.0