Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp556101pxb; Thu, 25 Feb 2021 09:05:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJwA1YPXyVPIqnfpgd/r5vIrnhpLcWSL2+6V4yNCTq21iFdyitURapB+ZH9e439vZibDF0If X-Received: by 2002:a17:907:d0b:: with SMTP id gn11mr3557199ejc.144.1614272718407; Thu, 25 Feb 2021 09:05:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614272718; cv=none; d=google.com; s=arc-20160816; b=Qa6R6lVWGyESKypJ4yssEW7qjBxIBEt+70EOTx5bKad0/l4Qu0r0CM0AoPz2rQAl2g Xq3hilPYRvI4UA/1JD4sUeaFQqUuTPtJaC8B5NeFMdO7c28stjjkyH0srjd6QsBzkzDd VOd/lGpt6yiE+BqRthgVr9lmctYeUPm93a2mRdYgcq1QVyOQvQ4P7OtWzZ8o7gDleo7t u4Ov/tSmB9k/qip3ci7W2Ekwyhyist4YghtaHV2D243/+tYlIPl92IuSOMFXumL+mJES csjhZDvb2IWbcSknXkgwl3ArdFaVQoHrfrSnAim2jeDOdpAMUO5ZQ9qYeXEl0lsTjb41 XMNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=XYtICfMRgoeAzpAFxndFi/RqF97bpRlSBAuvWvYjYgM=; b=1Gju7d0iraUL7gzENgsKuogZ86F9r6DquFq7fBxUXObw0qfvcZSBGyj3U+GYKu+XW9 4dbY8X4C0+QrB0EmFAPPRX7bIjpdsQaeJZOanN9eY9ZX8G39vqbIIBl/W5UbgA3M2uoQ 1pE6hxQPMeNBlBSUREwI17F1F0H8CDcHTS4fMwCi7OCWbsUPrLHHSQSyPcVEjRyN13A5 LpPnqf1E9OL4d6cFIf3nTj1NmOWH5AMhOT1XGULeG0gm/eaPKOsde5I3To9IrzfHB8DY Sp8KHPHwrCU30wpkuNXr9BpWFQZM1ndeleDHdR9X3QzwIeXfSzso14z2/z3lWxoaWtWa Mtyg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i12si3566194edk.461.2021.02.25.09.04.45; Thu, 25 Feb 2021 09:05:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233929AbhBYRD6 (ORCPT + 99 others); Thu, 25 Feb 2021 12:03:58 -0500 Received: from outgoing-auth-1.mit.edu ([18.9.28.11]:58218 "EHLO outgoing.mit.edu" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234102AbhBYRDZ (ORCPT ); Thu, 25 Feb 2021 12:03:25 -0500 Received: from cwcc.thunk.org (pool-72-74-133-215.bstnma.fios.verizon.net [72.74.133.215]) (authenticated bits=0) (User authenticated as tytso@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 11PH23xr002652 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Feb 2021 12:02:04 -0500 Received: by cwcc.thunk.org (Postfix, from userid 15806) id 51FA015C343C; Thu, 25 Feb 2021 12:02:03 -0500 (EST) Date: Thu, 25 Feb 2021 12:02:03 -0500 From: "Theodore Ts'o" To: harshad shirwadkar Cc: Zhiqiang Liu , Ext4 Developers List , linfeilong , lihaotian , "lijinlin (A)" Subject: Re: [PATCH] debugfs: fix memory leak problem in read_list() Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Thu, Feb 25, 2021 at 07:51:09AM -0800, harshad shirwadkar wrote: > On Sat, Feb 20, 2021 at 12:41 AM Zhiqiang Liu wrote: > > > > > > In read_list func, if strtoull() fails in while loop, > > we will return the error code directly. Then, memory of > > variable lst will be leaked without setting to *list. > > > > Signed-off-by: Zhiqiang Liu > > Signed-off-by: linfeilong > > --- > > debugfs/util.c | 12 ++++++++---- > > 1 file changed, 8 insertions(+), 4 deletions(-) > > > > diff --git a/debugfs/util.c b/debugfs/util.c > > index be6b550e..9e880548 100644 > > --- a/debugfs/util.c > > +++ b/debugfs/util.c > > @@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len) > > > > errno = 0; > > y = x = strtoull(tok, &e, 0); > > - if (errno) > > - return errno; > > + if (errno) { > > + retval = errno; > > + break; > > + } > Shouldn't we have `goto err;` here instead of break? strtoull failure > here indicates that no valid value was found, so instead of returning > the allocated memory, we should just free the memory and return error. As of commit 462c424500a5 ("debugfs: fix memory allocation failures when parsing journal_write arguments") there is no longer the err: goto target. The goal is to move to a model where the caller is exclusively responsible for freeing any allocated memory, since if realloc() has gotten into the act, the memory pointed to in *list would have been freed by realloc(). The fix is to make sure *list is updated before we return. This also allows the caller to have access to the list of numbers parsed before we ran into an error. So the Zhiqiang's patch is correc, and I will apply it. - Ted