Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3331079pxj; Mon, 24 May 2021 04:22:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBaiLvEMYUIeIsHK5vFeOCpoyJmeJ+8Y+oB+k6QCGUr95fsLqN+SmEBB1N8SwrniVQAXBO X-Received: by 2002:a50:f385:: with SMTP id g5mr25612905edm.222.1621855368675; Mon, 24 May 2021 04:22:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621855368; cv=none; d=google.com; s=arc-20160816; b=PyPydldeEv337MM26ZR14Gh0nHNe9n7k6BMGRaTJzfm6bLtWhDIVtWjQ2zLDgShu2x MgRgvRDw5H+kMPRgWyjQi53XV+38FZX24WoSD1cvbsrf8KLL6FdoNgB3+FMwsIHMy0lL fpLtnJZYYhd25OMFEcAyfINl11nvkLR8QR1XUEMyYOzLLpBw4gDF8NCM/h0UePtC4o81 3gpB4+zKrW4XaE8tPLXnYUHaFVWgN1ROZQteHHic/+B+FZv/kHEg45m67HXqGFZwuDrh I9lRc9Ut/ioHXE56LmMdEh7fzjnWDjFoDS6Z3Yso6NEhZM/8sgsnu0V192+nn8yQuTAh Argw== 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=EGRwGSFKo+OAdacy5psy6GPlGFHGtY9oQVJbaZ3xSD0=; b=ii3Cm6djn9HP7wkYkBKHO4gUCyLtC9zxZUThG06oIYqTHE9lQGM46Bg7LbfyfPoh3q 7FWWa8W01rXDFED2OmFHFjDSHXn7ltafW6RJclnVnNGM3x9X8nQEO9gTyKzD+xiBY6tT J8HOFa0Tp4I4z1aqB/fU3PnsgydbCjkfZ6ZxPd8bVoUIHbinLDwWL1zhUV8UdwkWcMsP jR8NRS15fpen2jYnt+u8pgU7hXvo1a08z+T+wcVGaSGhKPAOG6/bZ0SrT3k78/TB5l7Q d2W3Jpreyp2TFQ2nTHOxdPTMFLer4McYmLsT2m8i+r/qHeEG/gk3KgxCS1xKXpOydZUN HlGA== 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 h17si12829183edw.68.2021.05.24.04.22.25; Mon, 24 May 2021 04:22: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 S232574AbhEXLXM (ORCPT + 99 others); Mon, 24 May 2021 07:23:12 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:5536 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232517AbhEXLXL (ORCPT ); Mon, 24 May 2021 07:23:11 -0400 Received: from dggems702-chm.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4FpZTm2r5YzkYyP for ; Mon, 24 May 2021 19:18:52 +0800 (CST) Received: from dggpemm500014.china.huawei.com (7.185.36.153) by dggems702-chm.china.huawei.com (10.3.19.179) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 24 May 2021 19:21:42 +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, 24 May 2021 19:21:42 +0800 Subject: [PATCH 05/12] ss_create_invocation: fix memory leak and check whether NULL pointer From: Wu Guanghao To: CC: , References: <266bc52e-e279-ce84-0e1f-1405b9bc6174@huawei.com> Message-ID: Date: Mon, 24 May 2021 19:21:41 +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: <266bc52e-e279-ce84-0e1f-1405b9bc6174@huawei.com> Content-Type: text/plain; charset="gbk" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.184] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) 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_create_invocation(), it is necessary to check whether returned by malloc is a null pointer. Signed-off-by: Wu Guanghao Signed-off-by: Zhiqiang Liu --- lib/ss/invocation.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/ss/invocation.c b/lib/ss/invocation.c index 457e7a2c..cfc180a5 100644 --- a/lib/ss/invocation.c +++ b/lib/ss/invocation.c @@ -29,26 +29,31 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string, register int sci_idx; register ss_data *new_table; register ss_data **table; + register ss_data **realloc_table; *code_ptr = 0; table = _ss_table; new_table = (ss_data *) malloc(sizeof(ss_data)); + if (new_table == NULL) + goto out; if (table == (ss_data **) NULL) { table = (ss_data **) malloc(2 * size); + if (table == NULL) + goto free_new_table; table[0] = table[1] = (ss_data *)NULL; } initialize_ss_error_table (); for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++) ; - table = (ss_data **) realloc((char *)table, + realloc_table = (ss_data **) realloc((char *)table, ((unsigned)sci_idx+2)*size); - if (table == NULL) { - *code_ptr = ENOMEM; - free(new_table); - return 0; - } + if (realloc_table == NULL) + goto free_table; + + table = realloc_table; + table[sci_idx+1] = (ss_data *) NULL; table[sci_idx] = new_table; @@ -57,9 +62,15 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string, new_table->argv = (char **)NULL; new_table->current_request = (char *)NULL; new_table->info_dirs = (char **)malloc(sizeof(char *)); + if (new_table->info_dirs == NULL) + goto free_table; + *new_table->info_dirs = (char *)NULL; new_table->info_ptr = info_ptr; new_table->prompt = malloc((unsigned)strlen(subsystem_name)+4); + if (new_table->prompt == NULL) + goto free_info_dirs; + strcpy(new_table->prompt, subsystem_name); strcat(new_table->prompt, ": "); #ifdef silly @@ -71,6 +82,9 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string, new_table->flags.abbrevs_disabled = 0; new_table->rqt_tables = (ss_request_table **) calloc(2, sizeof(ss_request_table *)); + if (new_table->rqt_tables == NULL) + goto free_prompt; + *(new_table->rqt_tables) = request_table_ptr; *(new_table->rqt_tables+1) = (ss_request_table *) NULL; @@ -85,6 +99,18 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string, ss_get_readline(sci_idx); #endif return(sci_idx); + +free_prompt: + free(new_table->prompt); +free_info_dirs: + free(new_table->info_dirs); +free_table: + free(table); +free_new_table: + free(new_table); +out: + *code_ptr = ENOMEM; + return 0; } void --