Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp670955rdh; Sun, 24 Sep 2023 07:01:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHt0gFMCqnOmLoSGTC2YL2S/iq0O8mA7tmOAiWtn8ZqIQn0smROSI4BFbwNu7x6bIxwfyJA X-Received: by 2002:a05:6358:5288:b0:142:fd2b:d165 with SMTP id g8-20020a056358528800b00142fd2bd165mr6549751rwa.13.1695564068486; Sun, 24 Sep 2023 07:01:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695564068; cv=none; d=google.com; s=arc-20160816; b=fB3YEEXeKgGBTCEUdrsN1wmsESjpV5hXD5BJ8JGjZuBrcU0NCBF6J5V7rQjnYD9oRG 9yRfWwRQb37IAYUXm149OIoNFdiEzJVBMFPN4tMnP5tedefMZjYMQyMh0CjsOLYUrlEM jY0vH5RZpYgPq1+FMPqOCC9G/EXih9WOQ6Krxa2mHjA7rp6iSpEZYTwqtdu7Xw3xbRRp CoACF/dda2ye0WZ6One/lArp9Px2DO7C2LjUXuBOxumPsjdQc/nhDPtKN7n8uwipRXTQ LtAFOuggtoW0BlJU9doF1Z9OeYREBWE97DfEPALH/I+Xsm2KttKU88jwThW45EqE1YqO yxQw== 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:dkim-signature; bh=VrHTSpkzd+LXtpKuymSYv3mpDomhlVl0YG5JvuCE3wQ=; fh=6GcOuaYO85H8H4rL0gTKsicsMuMAMGriyEborH4Zu6k=; b=aAzN0CqsdAR9zmdX9QoMmbO56OH1FswAKGqqYHdWN1FLAhbZ9olVx+fEdkqgwm2CLb FiDoeL8CXmcedgfy8jaTNEhOQssHzb2GRMw++SztOrXn/VkriNa8mflLHUHqdvJ6ni8r XD7RdGqslZDNLCqZwosmdWrrG4JEMNUmI29dKaDPNCyketd2zZ1E3sJwPom9DUodE4CK ZpI9hFw6xSx9JZN7cZDiOVVo1pvrUWqc4yo3SNUKWvcNETbL2Bl33bQ0YOZhFdBC+Ntg 59dSuT+/2WGXA78GZ4zBII68vAVNuoxIb2iIN42JM3PvHupaaWAzC1I3BRjbKb73vEVA owGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=O4hjqhs9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id s20-20020a632c14000000b00578dff7fccbsi7810941pgs.137.2023.09.24.07.01.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 07:01:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=O4hjqhs9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id C8421806138E; Sat, 23 Sep 2023 23:50:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229549AbjIXGua (ORCPT + 99 others); Sun, 24 Sep 2023 02:50:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbjIXGu3 (ORCPT ); Sun, 24 Sep 2023 02:50:29 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A94ACA; Sat, 23 Sep 2023 23:50:22 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-690f2719ab2so1137341b3a.0; Sat, 23 Sep 2023 23:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695538222; x=1696143022; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VrHTSpkzd+LXtpKuymSYv3mpDomhlVl0YG5JvuCE3wQ=; b=O4hjqhs9/opvQsxqLtmcgws1LtdSUnYuVc7YS293GkiYbAp8qnfoQk5haY0p13X/5O d0a9cfswjmVLFyfEtyCEheTjAkoc2VLtVx2Y4f3YK4BBU9mLt92f+NHSwm3xc0BzOZ07 VT/16d3eYD6xpNkd7oG56RCJCSlCkFWyml8jtFDuWjWhU/WZFCTCNenRrLyAKA2vqh/t Ab5BfwRcgTE5wSIdH8vpSho3BZbI6F+EFtVuNFOXTO8RcQcl3P+AdZA+WmLxKsmadqAt dRHPPXcECPbdFU1Bp8TWdEDOsAoGsyYaixzbrUufcgjh/etDl5DwgOBy7sfLWXgHbEko D/sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695538222; x=1696143022; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VrHTSpkzd+LXtpKuymSYv3mpDomhlVl0YG5JvuCE3wQ=; b=dno0nyKWdulrwDQiJGwwdS9sUdaK+CKWkH5CMH9rE9lEpnIcie8s3qn6RTarQTT17i 50Pz+GQhqSKiNDeSpOMqFf2PGrfVumdgGtYBdlmoODSrqG+CosWY8jJjxxqEfw9T1C++ IZXgzq9rFF8lyF0+/fKTTurujeW+AaJvMrq0OBIv/V0VTpJzcD08DQTFJWXBdAGbBqah am8kKt0jhq23X1TzJuS0pnvTThsfbNRaTKj8a7c3Un8MfGI2SdV0NgIrur4nujo8oPbo MieWMGzXYLtFLZlRcHAslx3krbiRI7lukDnqAB+kxFf6ZWQKCWlgAHcyxUOOoUrQqQ7s aagQ== X-Gm-Message-State: AOJu0YxDQysGQbXtw5cTJexduVLmHa36LtbcN2AaL+tRFfyBdjbVFJdW RGBVaB7Sk+CTqOIN6ONtpnPXsRVVGbKUa7vz X-Received: by 2002:a05:6a00:1d8a:b0:68e:25ff:613e with SMTP id z10-20020a056a001d8a00b0068e25ff613emr4485965pfw.3.1695538221989; Sat, 23 Sep 2023 23:50:21 -0700 (PDT) Received: from localhost.localdomain ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id w3-20020aa78583000000b0068fc6570874sm5828899pfn.9.2023.09.23.23.50.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 23:50:21 -0700 (PDT) From: Kuan-Wei Chiu To: rafael@kernel.org Cc: daniel.lezcano@linaro.org, amitk@kernel.org, rui.zhang@intel.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH] tools/thermal: fix memory leak in realloc failure handling Date: Sun, 24 Sep 2023 14:50:13 +0800 Message-Id: <20230924065013.1081471-1-visitorckw@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sat, 23 Sep 2023 23:50:26 -0700 (PDT) In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed realloc operation. This patch addresses the problem by releasing the old memory before setting the pointer to NULL in case of a realloc failure. This ensures that memory is properly managed and avoids potential memory leaks. Signed-off-by: Kuan-Wei Chiu --- tools/thermal/lib/mainloop.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/thermal/lib/mainloop.c b/tools/thermal/lib/mainloop.c index 94cbbcbd1c14..6dcc4090d47e 100644 --- a/tools/thermal/lib/mainloop.c +++ b/tools/thermal/lib/mainloop.c @@ -62,9 +62,13 @@ int mainloop_add(int fd, mainloop_callback_t cb, void *data) struct mainloop_data *md; if (fd >= nrhandler) { - mds = realloc(mds, sizeof(*mds) * (fd + 1)); - if (!mds) + struct mainloop_data **mds_tmp = + realloc(mds, sizeof(*mds) * (fd + 1)); + if (!mds_tmp) { + free(mds); return -1; + } + mds = mds_tmp; nrhandler = fd + 1; } -- 2.25.1