Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp936282pxv; Thu, 15 Jul 2021 20:28:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpGORGgAnTmouhO75VXfY/l7QgW6i9nBCQ3xBi2oGjcB1F7blKat8RZwuEvs3sb6q1I51J X-Received: by 2002:a17:907:3e02:: with SMTP id hp2mr9273457ejc.4.1626406102780; Thu, 15 Jul 2021 20:28:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626406102; cv=none; d=google.com; s=arc-20160816; b=DaQlP3rYhMnuT2XIuN9sUX+YPhxyAqFIP11trhmR/ygGv3jjLbFarPxK0xkJUteBB8 Dk4TM+k8ZQpL8Motredr+G2lEStRiMN08l+d9u9zqmCKOJoz9xNn43svqMrsNzuTwHjM Hc1sYzVh6OBhu+ih6A5JnSV6bQZuJgoqn02RpNcPT7G662jiYRJBiWCc9xQsYdfqrSzl XhAJCaqCeQGWRP5Y3s9trXw6V+y8CxDqnbfwkyHXOfxmtDC7V2SE2YQ1o0YXOk9z3T5T RTobKPu6s0Gc0R553TUx1gNGTlZmWkSUJ1xIQQhBN4szehdbxykajCQZNN78TaZUQH8m qF+A== 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=ygLduEnz5cAvjZOUhT15qFJwuLOsR+sq0wTki3ARrqk=; b=leojVam4ty57BR1I1V5z0D5z2/jMN0EvVWsgtwA1dtyKgIg/WDVDWVL1ZsLD5x03Jr OrW+3wePS3bjhPMlTw22YJdkEK7+v3PPVRYRnwAxA8pkB9QiiDHKYuBCTe0zN+47NYKN mEjzClwt0MkqPk51761+lB7G/Q6BDu7SIu8ENavFNL+3beJsMOJtU1h/yPErG8poY2AD /IIw6+tsGrKBo5by6hRgTL2etwtb0iEiGiOm/nPgEcEbQXKG1lIr0zaXhjnPjS1BLxok 1kVC1HNhfYb1hjg+xeG4shZkkeF6c6aeT5RjjA0YZrCLv3b9oGtGz+HB6qTUrcXp4uI/ 4Nsg== 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 d24si9247952eds.184.2021.07.15.20.27.56; Thu, 15 Jul 2021 20:28:22 -0700 (PDT) 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 S231481AbhGPDat (ORCPT + 99 others); Thu, 15 Jul 2021 23:30:49 -0400 Received: from outgoing-auth-1.mit.edu ([18.9.28.11]:37210 "EHLO outgoing.mit.edu" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231230AbhGPDas (ORCPT ); Thu, 15 Jul 2021 23:30:48 -0400 Received: from callcc.thunk.org (96-65-121-81-static.hfc.comcastbusiness.net [96.65.121.81]) (authenticated bits=0) (User authenticated as tytso@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 16G3RjJx020669 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jul 2021 23:27:46 -0400 Received: by callcc.thunk.org (Postfix, from userid 15806) id F2A554202F5; Thu, 15 Jul 2021 23:27:44 -0400 (EDT) Date: Thu, 15 Jul 2021 23:27:44 -0400 From: "Theodore Y. Ts'o" To: wuguanghao Cc: linux-ext4@vger.kernel.org, artem.blagodarenko@gmail.com, liuzhiqiang26@huawei.com, linfeilong@huawei.com Subject: Re: [PATCH v2 04/12] ss_add_info_dir: fix memory leak and check whether Message-ID: References: <20210630082724.50838-2-wuguanghao3@huawei.com> <20210630082724.50838-5-wuguanghao3@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210630082724.50838-5-wuguanghao3@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Wed, Jun 30, 2021 at 04:27:16PM +0800, wuguanghao wrote: > In ss_add_info_dir(), need free info->info_dirs before return, > otherwise it will cause memory leak. At the same time, it is necessary > to check whether dirs[n_dirs] is a null pointer, otherwise a segmentation > fault will occur. > > Signed-off-by: Wu Guanghao > Signed-off-by: Zhiqiang Liu > Reviewed-by: Wu Bo > --- > lib/ss/help.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/lib/ss/help.c b/lib/ss/help.c > index 5204401b..6768b9b1 100644 > --- a/lib/ss/help.c > +++ b/lib/ss/help.c > @@ -148,6 +148,7 @@ void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr) > dirs = (char **)realloc((char *)dirs, > (unsigned)(n_dirs + 2)*sizeof(char *)); > if (dirs == (char **)NULL) { > + free(info->info_dirs); > info->info_dirs = (char **)NULL; > *code_ptr = errno; > return; Adding the free() isn't right fix. The real problem is that this line should be removed: info->info_dirs = (char **)NULL; The function is trying to add a string (a directory name) to list, and the realloc() is trying to extend the list. If the realloc fils, we shouldn't be zapping the original list. We should just be returning, and leaving the original list of directories unchanged and untouched. - Ted