Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3643448rwe; Mon, 17 Apr 2023 00:59:13 -0700 (PDT) X-Google-Smtp-Source: AKy350ZMGk8ekQVVOpa4Z1Ast210qXIJLJLyh1dsU/OgWw2WVL8CZVTU10UXkEBgReT9xIGkS5lX X-Received: by 2002:a17:90b:18f:b0:23f:962e:826b with SMTP id t15-20020a17090b018f00b0023f962e826bmr13937319pjs.15.1681718352861; Mon, 17 Apr 2023 00:59:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681718352; cv=none; d=google.com; s=arc-20160816; b=WorXI3aWmjM68sTRm2bGXjFgqLcm1ByxtkwI5x0VB+k2dGIhm9ViEMjcryhp95N3Ky FRur/2zsorUWXmeeS8dY7VSTu/OVlsir9U34d2whbHVJsdwYkWy3HHMzxmvpzgL3Mnvl tG5vvko/x+7StkZMxllZsTbqvmhMf2zqy4yxfWIQVFQge73R+y6KcBh+Wx2DKShmHKUP b28Ye8Ev9BgTuyQ4Bg0kcBbcld23nfa8mUpy9RZmv9NDJgAA7ZG5mlpJUjQBYW1dpYal OwSVdTna9/MaQ9lWJJqy34vHtoa01ojV4kMZN9eFQOW2HmAaWImaB30seyJRxoDtFl6Z oIwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=mwN1i3oZ12ukn9mciW7sg3J/k9K/9b+oV00MQx6RRRU=; b=KHatV7mwPgzSIogZj5xQBxzyjwCBn/aSSdN97fpLpsvZ+CzSDrMVFvQ3sNZA2g5poZ Ls1JIFjcjwQ09Vg44mQuNUNUe3sFZ4L8YmXVKfcoRJX4snQ6AUEvhyxkpJCi0WYmgEYv RxrmpANimscFpy1d0vLjPqwuqfpJKEKR8LnAoURV3OphxnO79k17gv6FiTpkV6jvLGlI cCayPmY6ZSFyMEOl1RVkJpIyObl8mGF0PRSKROk1sSywy/zZud5R7DcGx7jQ90qkQstn n26BSgD/GkRzmIFVtk5LQNktMfuUViXOM/3lrbyk6Keuiv3dqSFZJSNT+q6fGcbd2Ru5 VPJw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 21-20020a631955000000b0051414166b3csi11088333pgz.406.2023.04.17.00.58.59; Mon, 17 Apr 2023 00:59:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231270AbjDQH6O (ORCPT + 99 others); Mon, 17 Apr 2023 03:58:14 -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 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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/l= ib/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 =3D 0; =20 *mode =3D 0; =20 fd =3D open(path, O_RDONLY); - if (fd =3D=3D -1) - return -1; + if (fd =3D=3D -1) { + ret =3D -1; + goto out; + } =20 if (read(fd, &yes_no, 1) !=3D 1) { - close(fd); - return -1; + ret =3D -1; + goto out_close; } =20 if (yes_no =3D=3D '1') { *mode =3D 1; - return 0; - } else if (yes_no =3D=3D '0') { - return 0; + } else if (yes_no !=3D '0') { + ret =3D -1; + goto out_close; } - return -1; +out_close: + close(fd); +out: + return ret; } =20 int powercap_get_enabled(int *mode) --=20 2.37.2