From patchwork Wed Mar 2 16:31:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin King X-Patchwork-Id: 8483591 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 170289F2F0 for ; Wed, 2 Mar 2016 16:31:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 20FAA2037E for ; Wed, 2 Mar 2016 16:31:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7D2712034E for ; Wed, 2 Mar 2016 16:31:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752930AbcCBQb2 (ORCPT ); Wed, 2 Mar 2016 11:31:28 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:56568 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752713AbcCBQb1 (ORCPT ); Wed, 2 Mar 2016 11:31:27 -0500 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1ab9gL-0007N1-U5; Wed, 02 Mar 2016 16:31:25 +0000 From: Colin King To: Thomas Renninger , linux-pm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] cpupower: bench: parse.c: fix several resource leaks Date: Wed, 2 Mar 2016 16:31:25 +0000 Message-Id: <1456936285-22915-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 2.7.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Colin Ian King The error handling in prepare_output has several issues with resource leaks. Ensure that filename is free'd and the directory stream DIR is closed before returning. Signed-off-by: Colin Ian King --- tools/power/cpupower/bench/parse.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c index f503fb5..2d09c92 100644 --- a/tools/power/cpupower/bench/parse.c +++ b/tools/power/cpupower/bench/parse.c @@ -81,14 +81,18 @@ FILE *prepare_output(const char *dirname) len = strlen(dirname) + 30; filename = malloc(sizeof(char) * len); + if (!filename) { + perror("malloc"); + goto out_dir; + } if (uname(&sysdata) == 0) { len += strlen(sysdata.nodename) + strlen(sysdata.release); filename = realloc(filename, sizeof(char) * len); - if (filename == NULL) { + if (!filename) { perror("realloc"); - return NULL; + goto out_dir; } snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log", @@ -104,12 +108,16 @@ FILE *prepare_output(const char *dirname) if (output == NULL) { perror("fopen"); fprintf(stderr, "error: unable to open logfile\n"); + goto out; } fprintf(stdout, "Logfile: %s\n", filename); - free(filename); fprintf(output, "#round load sleep performance powersave percentage\n"); +out: + free(filename); +out_dir: + closedir(dir); return output; }