From patchwork Mon Apr 17 07:56:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Zeng X-Patchwork-Id: 674412 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3C8FC77B72 for ; Mon, 17 Apr 2023 07:58:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231167AbjDQH6N (ORCPT ); Mon, 17 Apr 2023 03:58:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230491AbjDQH5p (ORCPT ); Mon, 17 Apr 2023 03:57:45 -0400 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C6223594; Mon, 17 Apr 2023 00:57:37 -0700 (PDT) X-UUID: c289f049c77a4a198bb1e1d0d4378632-20230417 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22, REQID:7ce7aaf4-ce7a-4d63-a731-4be079b546fb, IP:-32 768,URL:-32768,TC:-32768,Content:-32768,EDM:-32768,RT:-32768,SF:-32768,FIL E:-32768,BULK:-32768,RULE:Release_Ham,ACTION:release,TS:0 X-CID-INFO: VERSION:1.1.22, REQID:7ce7aaf4-ce7a-4d63-a731-4be079b546fb, IP:-3276 8,URL:-32768,TC:-32768,Content:-32768,EDM:-32768,RT:-32768,SF:-32768,FILE: -32768,BULK:-32768,RULE:Release_Ham,ACTION:release,TS:0 X-CID-META: VersionHash:120426c, CLOUDID:nil, BulkID:nil, BulkQuantity:0, Recheck: 0,SF:nil,TC:nil,Content:nil,EDM:nil,IP:nil,URL:nil,File:nil,Bulk:nil,QS:ni l,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: c289f049c77a4a198bb1e1d0d4378632-20230417 Received: from mail.kylinos.cn [(39.156.73.10)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 1100353138; Mon, 17 Apr 2023 15:56:21 +0800 Received: from mail.kylinos.cn (localhost [127.0.0.1]) by mail.kylinos.cn (NSMail) with SMTP id 4763EE0084A1; Mon, 17 Apr 2023 15:56:21 +0800 (CST) X-ns-mid: postfix-643CFBA5-20992179 Received: from zdzh5-QiTianM428-A376.. (unknown [172.20.12.253]) by mail.kylinos.cn (NSMail) with ESMTPA id 7FF1FE0084A1; Mon, 17 Apr 2023 15:56:19 +0800 (CST) From: Hao Zeng To: skhan@linuxfoundation.org Cc: trenn@suse.com, shuah@kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, zenghao@kylinos.cn Subject: [PATCH v3] cpupower:Fix resource leaks in sysfs_get_enabled() Date: Mon, 17 Apr 2023 15:56:17 +0800 Message-Id: <20230417075617.10487-1-zenghao@kylinos.cn> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The sysfs_get_enabled() opened file processor not closed, may cause a file handle leak. Putting error handling and resource cleanup code together makes the code easy to maintain and read. Removed the unnecessary else if branch from the original function, as it should return an error in cases other than '0'. Signed-off-by: Hao Zeng Suggested-by: Shuah Khan --- tools/power/cpupower/lib/powercap.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c index 0ce29ee4c2e4..f0334a5f1acf 100644 --- a/tools/power/cpupower/lib/powercap.c +++ b/tools/power/cpupower/lib/powercap.c @@ -40,25 +40,31 @@ static int sysfs_get_enabled(char *path, int *mode) { int fd; char yes_no; + int ret = 0; *mode = 0; fd = open(path, O_RDONLY); - if (fd == -1) - return -1; + if (fd == -1) { + ret = -1; + goto out; + } if (read(fd, &yes_no, 1) != 1) { - close(fd); - return -1; + ret = -1; + goto out_close; } if (yes_no == '1') { *mode = 1; - return 0; - } else if (yes_no == '0') { - return 0; + } else if (yes_no != '0') { + ret = -1; + goto out_close; } - return -1; +out_close: + close(fd); +out: + return ret; } int powercap_get_enabled(int *mode)