Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1480000rdh; Mon, 25 Sep 2023 14:16:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7u+FVGEkNAILVSdEY9cUCGOjMlWlp+dEIe1a2Iiu9WujAaXXe/vNa7tBcZcTypeaWhxQm X-Received: by 2002:a17:90a:540d:b0:26b:5461:8dd5 with SMTP id z13-20020a17090a540d00b0026b54618dd5mr4944499pjh.42.1695676564017; Mon, 25 Sep 2023 14:16:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695676564; cv=none; d=google.com; s=arc-20160816; b=XOtD0lioRMhFt30wcMuAEoRMwSvtEQYwIlieDC8y/4bk8Y3d7HsIeHXPuF8gNTr4Gj Zmdv7GkDqggN+MkqSAGJU0N1SUEHBgkEAEPD7EkWQTfqHgV0Hw2W1/5Hx5ZnVz2qP2cs VIsYG60z8JYkU6P5IoeEabgCidFhIFHkd+EBlUfeY+wRnjVyovavhmDOvyCCuYWp1HAV xX0sywWrbjIA9KP6WYN2GCz+v8Ob+UECLA4T9+Bj0RVu1+gvOJJFj6YA/uu0yI+1u9IS uldJ4A2fKF6BS25KWHSbnVp6yHIqBxbpDcncKccjmZc7FUE17lmXqV7lZipbDbTCu4eU lfnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=2OvvcdbPDlc5RUM3ipWasxoHtTemWDcPqStni+63QcQ=; fh=79NMzyg5jWw1GC2ZsVvFItsiz02+VDawOkVE3uYKbzw=; b=FaJno02eGI29Bhqd60t6XQrxXwv5TAz34batTuPAlHrJut1K/aXphj1GRo+9yJO5Qm L7UJAAA4djR+keq0c6ar90wyJrjLs4X3t1XvLxXlRvFMK4+A8wUipFztLIX3V5GuWL9+ /UGX7UZUlZ4RKnt4349YZD9OWq1x2HrzrjmkUFfQVuE7ETU/yECEuongg8XNbHTWlht/ MAPf7tFF2gr5aDn0Sao6U2dvILHHc/cgHrIwnkzZeYO/RtmnWu9kf6ZFthShhe52QdJA Hn6l+voLxwecqquLuApnT6j6/hjlZPwYy1xVyJllfdTZqylTl1rp4h92Q7/rTBmMG5oo lNhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pXWZW3f0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id lx4-20020a17090b4b0400b002766354b7aesi14649761pjb.166.2023.09.25.14.16.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 14:16:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pXWZW3f0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 33B6680A1669; Mon, 25 Sep 2023 02:42:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229503AbjIYJmS (ORCPT + 99 others); Mon, 25 Sep 2023 05:42:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230328AbjIYJmN (ORCPT ); Mon, 25 Sep 2023 05:42:13 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A249ADA for ; Mon, 25 Sep 2023 02:42:06 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-405417465aaso47443485e9.1 for ; Mon, 25 Sep 2023 02:42:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695634925; x=1696239725; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=2OvvcdbPDlc5RUM3ipWasxoHtTemWDcPqStni+63QcQ=; b=pXWZW3f0tfSh9GBeREKcpZnyr6JSbRduFpypxxL51Q3B1F02JN84P+WnRLNr2m2MMf m9fAv34Fd2LOC9MtVnrjkix3O1Bmyf1DhrP+vgLTKWGBzRQ/QGP+4+aamXhDTkQaKm+F HXswwBvjvhtD/wCC/Sl7O53MECBC6Im2GnrchURMbKmpjYjDnZ+hXrFhslZdGCRxoD35 IMWNEKL+Udch1IiNGwexxdlaiKQ16k79yPoS+hRK7gsCEYbP/U0xbi8QBNv8DLUyeOuy ptq/TFpB2VYkBV5ljqNUvZnlo2AuL7+nqNSd4kN81mtBM+NYr1T0xiTwUjQc9s60BTkK JyGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695634925; x=1696239725; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2OvvcdbPDlc5RUM3ipWasxoHtTemWDcPqStni+63QcQ=; b=W4u7xL7raPB3DmqHQ2zHhPBo9pHaxAi2LSZTASe96+ahgODDp0l0nH0/foWvlctKRv tkg4AWsfV7hMCmudmj+rs+5INYrPtpkSW7gA0352AenWlkYSxck59LjOTj1o4OoqR4w/ 9L6NYe1UF3eJX/OpmrxTZP0+w07m7BTPcoWU7snHLpdhvLn3fV0JAgZjyc04zs/64WhI 6rUtAFNOV5X9glcFMaPkf1uby2vXXMM1FNZQyAmYcmA93u6n5RojKKZD7ymOPf41fyBT tmah8Be3k7IlRNbxLfLRcvnt4wM4zxFSvwnuZB79xFKZgluHwA4dVRbN1BNU2sJr33Y+ pkLA== X-Gm-Message-State: AOJu0YzG16DO2t3foVxTDhCt0jo01X1JlnmFkXAglYZXFYDxwoxEACac r9LiqmGrZgD0DzF52m9KbGLEFA== X-Received: by 2002:a7b:c4cb:0:b0:404:f9c1:d5d7 with SMTP id g11-20020a7bc4cb000000b00404f9c1d5d7mr6099794wmk.25.1695634924989; Mon, 25 Sep 2023 02:42:04 -0700 (PDT) Received: from ?IPV6:2a05:6e02:1041:c10:a927:1e5c:85b4:9da1? ([2a05:6e02:1041:c10:a927:1e5c:85b4:9da1]) by smtp.googlemail.com with ESMTPSA id u21-20020adfa195000000b00323293bd023sm2209859wru.6.2023.09.25.02.42.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Sep 2023 02:42:04 -0700 (PDT) Message-ID: <93c87b5c-ae8f-d27d-0342-faa884a3142e@linaro.org> Date: Mon, 25 Sep 2023 11:42:03 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH] tools/thermal: fix memory leak in realloc failure handling Content-Language: en-US To: Kuan-Wei Chiu , rafael@kernel.org Cc: amitk@kernel.org, rui.zhang@intel.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org References: <20230924065013.1081471-1-visitorckw@gmail.com> From: Daniel Lezcano In-Reply-To: <20230924065013.1081471-1-visitorckw@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,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 (howler.vger.email [0.0.0.0]); Mon, 25 Sep 2023 02:42:21 -0700 (PDT) Hi Kuan-Wei, On 24/09/2023 08:50, Kuan-Wei Chiu wrote: > 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. Thanks for reporting the issue and proposing the fix. The description is not accurate actually, neither the fix. What is happening is we are losing the pointer information as the 'mds' variable is a global variable. So the assignation will overwrite the current pointer if it fails. That leads to a NULL pointer dereference in the mainloop_del. Looking closer to the code, it seems 'mds' is not used as the stored information is not accessed. For my understanding, we can just remove the: static struct mainloop_data **mds and static unsigned short nrhandler; along with the associated code > 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; > } > -- Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog