Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2222585pxj; Sun, 30 May 2021 18:28:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBAeWSYCbuS5tjY+oMR3NRFWuLRjeW0AaLQdZtmDsy+SEjReqvq0KoKCgtS5VFgqyaJlaq X-Received: by 2002:a17:907:76d4:: with SMTP id kf20mr1094373ejc.164.1622424528981; Sun, 30 May 2021 18:28:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622424528; cv=none; d=google.com; s=arc-20160816; b=ELAsd3Nb611VdOQ+PTFRUBwkV+Zq3XMroIK7QdZAdYREmTN8TiYWmJpCBGniw2iOjI enfGQNPegVuXOG600y23o+BWx00U40Hoa2Ne+EfObOMck8W+iY0Tf36W1YFrBFLRlBOt SfQ/q2qCi38vAdBi1YgPzgj6XdN1eM5MSWKYiYELly/LNBo/ESEoQDc9X4J6pwUly3Yp hAvmxPY0ulCfD9dSW6XBh7r4rX8+j2DBIgGSu5Kk12Hj9RxMjICbIbFGb7HlsrTOeg3s QbeCWrGbgaKripzW3RAxRfa+CPurVTD3mgE4ujO4gA6CSQiLhH4n5Galx/CV9HVIR7ya DQDg== 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 :mime-version:user-agent:date:message-id:references:cc:to:from :subject; bh=j5ZqGkNqHcr420rCEaX6iKuM2h3x42S20owmhBGSfXE=; b=g4GIvY99vbEnyMfWDGCTqhccgjyEy8WDjvSbyiFFt8jL3btwbxff2y8d6Io2pP4PLQ yZ7V0a08OFx/+CsXta+kCp1jkz7TrFLrniZ+b7awqciuGBTNd+/yJiv/CvCuVmd5ibUz t9JcqEiTpXlXSk1rmMoxuedKmYH2d6S/FK5NvxkeUJTai8lNzqwOE9S9AikLqbzCtJgF IIJ7N1Iw148wnMZGleARHXME7sRTAsEDZ2G3T6/6kxLH0pxgkbsSJcLaKGelLHMeTbZ+ kF+pAMH3OITnirKtU4pQ1fRwR4G4vHjqFs1F+kzF0pUclWtF3xDOj8fjOLTuMMOtU2XJ 4Vfw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb8si13975929edb.249.2021.05.30.18.28.26; Sun, 30 May 2021 18:28:48 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229897AbhEaBaE (ORCPT + 99 others); Sun, 30 May 2021 21:30:04 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:2792 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229891AbhEaBaD (ORCPT ); Sun, 30 May 2021 21:30:03 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Ftcxq6SCxzWq5x for ; Mon, 31 May 2021 09:23:43 +0800 (CST) Received: from dggpemm500014.china.huawei.com (7.185.36.153) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 31 May 2021 09:28:23 +0800 Received: from [10.174.179.184] (10.174.179.184) by dggpemm500014.china.huawei.com (7.185.36.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 31 May 2021 09:28:23 +0800 Subject: [PATCH V2 04/12] ss_add_info_dir: fix memory leak and check whether,NULL pointer From: Wu Guanghao To: , =?UTF-8?B?0JHQu9Cw0LPQvtC00LDRgNC10L3QutC+INCQ0YDRgtGR0Lw=?= CC: , References: <00ad4a90-8a40-24c1-98d9-eb5f0da42436@huawei.com> Message-ID: <03398446-66ea-01cb-e21a-b4841f325254@huawei.com> Date: Mon, 31 May 2021 09:28:23 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.2.1 MIME-Version: 1.0 In-Reply-To: <00ad4a90-8a40-24c1-98d9-eb5f0da42436@huawei.com> Content-Type: text/plain; charset="gbk" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.184] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500014.china.huawei.com (7.185.36.153) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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..429f410e 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; @@ -155,6 +156,10 @@ void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr) info->info_dirs = dirs; dirs[n_dirs + 1] = (char *)NULL; dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1); + if (dirs[n_dirs] == (char *)NULL) { + *code_ptr = errno; + return; + } strcpy(dirs[n_dirs], info_dir); *code_ptr = 0; } --