From patchwork Fri Apr 14 03:08:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Zeng X-Patchwork-Id: 674454 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 50145C77B6E for ; Fri, 14 Apr 2023 03:09:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229628AbjDNDJJ (ORCPT ); Thu, 13 Apr 2023 23:09:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229493AbjDNDJI (ORCPT ); Thu, 13 Apr 2023 23:09:08 -0400 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D5401998; Thu, 13 Apr 2023 20:09:06 -0700 (PDT) X-UUID: 12db5403e36046049364e194e7c8e264-20230414 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22, REQID:5ce2be2d-84dd-428d-ac01-1c1a80930b4b, 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:5ce2be2d-84dd-428d-ac01-1c1a80930b4b, 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: 12db5403e36046049364e194e7c8e264-20230414 X-User: zenghao@kylinos.cn Received: from zdzh5-qitianm428-a376.. [(116.128.244.169)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 1123143396; Fri, 14 Apr 2023 11:08:32 +0800 From: Hao Zeng To: skhan@linuxfoundation.org Cc: trenn@suse.com, shuah@kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zeng Subject: [PATCH v2] cpupower:Fix resource leaks in sysfs_get_enabled() Date: Fri, 14 Apr 2023 11:08:30 +0800 Message-Id: <20230414030830.3829332-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 When the read return value is equal to 1, a file handle leak will occur Signed-off-by: Hao Zeng Suggested-by: Shuah Khan --- tools/power/cpupower/lib/powercap.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tools/power/cpupower/lib/powercap.c b/tools/power/cpupower/lib/powercap.c index 0ce29ee4c2e4..02ec5b0bff6b 100644 --- a/tools/power/cpupower/lib/powercap.c +++ b/tools/power/cpupower/lib/powercap.c @@ -40,7 +40,7 @@ static int sysfs_get_enabled(char *path, int *mode) { int fd; char yes_no; - + int ret = 0; *mode = 0; fd = open(path, O_RDONLY); @@ -48,17 +48,18 @@ static int sysfs_get_enabled(char *path, int *mode) return -1; if (read(fd, &yes_no, 1) != 1) { - close(fd); - return -1; + ret = -1; + goto err; } - if (yes_no == '1') { - *mode = 1; - return 0; - } else if (yes_no == '0') { - return 0; + if (yes_no != '1' || yes_no != '0') { + ret = -1; + goto err; } - return -1; + *mode = yes_no - '0'; +err: + close(fd); + return ret; } int powercap_get_enabled(int *mode)