From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753955AbbDGMme (ORCPT ); Tue, 7 Apr 2015 08:42:34 -0400 Received: from mail-wi0-f181.google.com ([209.85.212.181]:35207 "EHLO mail-wi0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753705AbbDGMma (ORCPT ); Tue, 7 Apr 2015 08:42:30 -0400 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= To: lidza.louina@gmail.com, markh@compro.net Cc: gregkh@linuxfoundation.org, driverdev-devel@linuxdriverproject.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, sudipm.mukherjee@gmail.com, =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Subject: [PATCH v2] staging: dgnc: check if kzalloc fails in dgnc_tty_init() Date: Tue, 7 Apr 2015 15:40:17 +0300 Message-Id: <1428410417-18524-1-git-send-email-giedrius.statkevicius@gmail.com> X-Mailer: git-send-email 2.3.5 In-Reply-To: <1428402392-15817-1-git-send-email-giedrius.statkevicius@gmail.com> References: <1428402392-15817-1-git-send-email-giedrius.statkevicius@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If one of the allocations of memory for storing a channel information struct fails then free all the successful allocations and return -ENOMEM that gets propogated to the pci layer. Also, remove a bogus skipping in the next part of the initiation if a previous memory allocation failed because we won't execute that if any of the allocations failed. Signed-off-by: Giedrius Statkevičius --- v2: Only returning -ENOMEM if an allocation failed isn't enough as it was spotted by Sudip so create a new label that frees all successfully allocated stuff and only then returns -ENOMEM. Also, remove a unnecessary check in the next loop. drivers/staging/dgnc/dgnc_tty.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/staging/dgnc/dgnc_tty.c b/drivers/staging/dgnc/dgnc_tty.c index ce4187f..60d7e49 100644 --- a/drivers/staging/dgnc/dgnc_tty.c +++ b/drivers/staging/dgnc/dgnc_tty.c @@ -316,6 +316,8 @@ int dgnc_tty_init(struct dgnc_board *brd) * interrupt context, and there are no locks held. */ brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL); + if (!brd->channels[i]) + goto err_free_channels; } } @@ -324,10 +326,6 @@ int dgnc_tty_init(struct dgnc_board *brd) /* Set up channel variables */ for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) { - - if (!brd->channels[i]) - continue; - spin_lock_init(&ch->ch_lock); /* Store all our magic numbers */ @@ -375,6 +373,11 @@ int dgnc_tty_init(struct dgnc_board *brd) } return 0; + +err_free_channels: + for (i = i - 1; i >= 0; --i) + kfree(brd->channels[i]); + return -ENOMEM; } -- 2.3.5