From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6354AC11F69 for ; Wed, 30 Jun 2021 08:27:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48A5261D0B for ; Wed, 30 Jun 2021 08:27:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233568AbhF3IaQ (ORCPT ); Wed, 30 Jun 2021 04:30:16 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:9327 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233543AbhF3IaO (ORCPT ); Wed, 30 Jun 2021 04:30:14 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4GFDrL5Zcrz74JM for ; Wed, 30 Jun 2021 16:23:30 +0800 (CST) Received: from dggpemm500014.china.huawei.com (7.185.36.153) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 30 Jun 2021 16:27:32 +0800 Received: from huawei.com (10.175.104.170) 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; Wed, 30 Jun 2021 16:27:32 +0800 From: wuguanghao To: , CC: , , Subject: [PATCH v2 04/12] ss_add_info_dir: fix memory leak and check whether Date: Wed, 30 Jun 2021 16:27:16 +0800 Message-ID: <20210630082724.50838-5-wuguanghao3@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210630082724.50838-2-wuguanghao3@huawei.com> References: <20210630082724.50838-2-wuguanghao3@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.170] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) 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..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; @@ -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; } -- 2.19.1