From 45b478c763bfaefbda5518d4e6900193d1e65ad0 Mon Sep 17 00:00:00 2001 From: Lisa Nguyen Date: Mon, 7 Nov 2016 12:44:10 -0800 Subject: Fix resource leak in output_cstate_info() and build_cstate_info() Coverity Scan detected a resource leak in build_cstate_info() and output_cstate_info() functions. Refer to defect #84131. Valgrind confirms the memory leak as well: ==41915== 4,768 bytes in 1 blocks are definitely lost in loss record 6 of 8 ==41915== at 0x4C2F948: calloc (vg_replace_malloc.c:711) ==41915== by 0x40232B: build_cstate_info (idlestat.c:318) ==41915== by 0x403DD1: output_cstate_info (idlestat.c:937) ==41915== by 0x404D53: idlestat_store (idlestat.c:1286) ==41915== by 0x4055A8: main (idlestat.c:1532) Fix this issue by rewritting the syntax for the calloc function inside build_cstate_info(), so the pointer can be easily deferenced later. Also, deallocate any memory that was created locally inside output_cstate_info(). Signed-off-by: Lisa Nguyen --- idlestat.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/idlestat.c b/idlestat.c index 5571ae8..93ac86a 100644 --- a/idlestat.c +++ b/idlestat.c @@ -315,7 +315,7 @@ struct cpuidle_cstates *build_cstate_info(int nrcpus) assert(nrcpus > 0); - cstates = calloc(nrcpus, sizeof(*cstates)); + cstates = (struct cpuidle_cstates *)calloc(nrcpus, sizeof(struct cpuidle_cstates)); if (!cstates) return ptrerror(__func__); @@ -947,6 +947,8 @@ void output_cstate_info(FILE *f, struct cpu_topology * topo, int nrcpus) cstates[i].cstate[j].target_residency); } } + + free(cstates); } #define TRACE_IRQ_FORMAT "%*[^[][%d] %*[^=]=%d%*[^=]=%16s" -- cgit v1.2.3