Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp126623pxv; Wed, 30 Jun 2021 01:28:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxaBU2PyfykQ3VK+EpvCvGeXr3as+M8+CcW+OKXn7qPRxUH1kimhT6Kb0ZTFRnF5it128+ X-Received: by 2002:a92:c546:: with SMTP id a6mr2248460ilj.205.1625041692487; Wed, 30 Jun 2021 01:28:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625041692; cv=none; d=google.com; s=arc-20160816; b=XlLJXzOBgRl+/tzaPWNA9+VcOX2lp09nGpa4HhNVM/p61+vfFS6T8LkAoxWzI6yOR7 R4Wr4mBWlfGrnJ6pvFPP0zczdqcWHiku+6Jq5PDqf1xPfB/6ZL/xo1jY23Mz1ExvwmNu 6Gvjy2ODAj0IxA4Rxq8cu18JpqQAEK+mwEzRAnvacXDVz6cl1cXadNVluQybWojKO6XA ig7wjMOTHO0m/2234WSXU1fRDQUUbu55+hZKOG+P8qhUfg4IJoOn5xdDLfjgY7ZegxFF HhCgNiIyJPEy4c2wU/9KN2FIWw/CvjvRDZ6b7hK6EnRKNZI8+2hYjqR8Hy1aMIuR6Jqv 6eHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=fAkkeVusNdKQvj6QvYvtubdqGsf0KiZV0qHFNxtC/98=; b=pe+shUvuQlbtxVAgDLsJoGGwAEwISXATqjxPSvr9fLJh0TWGX7WAa4jVVt82fVh32N ClrzdIK3qILXz8JIuTG0okdg6hR4PWJXp6efEidZtp8WrT4B/RmFC40EUTXXoA2B8oH+ Gz6fzfyO3pYqMscatfy71fQQy5dgpNarZFjR8km73gnJiZUHD02LY8DspZqP2gHHN/Ey BW0IjDnGWcGg1oWt9Lx/b3inKEazQz7YVBPKup/7CdJTpx6WvLY+iJJzdTTwBvem+vQJ A62+awwdGfLNbKU6fRrNQxH5+OHe0rWOgvlJhXkR5hfe2l880Hfh6OwYHNoRr0Fj19Bc 5Plg== 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 m4si23804191ioy.95.2021.06.30.01.28.01; Wed, 30 Jun 2021 01:28:12 -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 S233519AbhF3IaQ (ORCPT + 99 others); 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