All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Giedrius Statkevičius" <giedrius.statkevicius@gmail.com>
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,
	dan.carpenter@oracle.com, sudipm.mukherjee@gmail.com,
	"Giedrius Statkevičius" <giedrius.statkevicius@gmail.com>
Subject: [PATCH v5 1/3] staging: dgnc: clean up allocation of ->channels[i]
Date: Fri, 10 Apr 2015 02:42:29 +0300	[thread overview]
Message-ID: <1428622951-31061-1-git-send-email-giedrius.statkevicius@gmail.com> (raw)
In-Reply-To: <1428423102-29138-1-git-send-email-giedrius.statkevicius@gmail.com>

Check if kzalloc fails in dgnc_tty_init() and if it does then free all
previously allocated ->channels[i] and set them to NULL. This makes the code
less error/bug prone because instead of needing programmers attention to add
checks everywhere we do that in one place. Also, remove a bogus comment and
check in the same loop because ->channels[i] isn't allocated anywhere else.
Finally, remove a unnecessary check if ->channels[i] is NULL in the next loop
because it can't be.

Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@gmail.com>
---
v5: no change

v4: Make this patch only for dgnc_tty.c and only for this thing. Split into 3
patches (1 patch fixes a bug reported by Dan Carpenter)

v3: Remove the wrong comment at dgnc_tty_init() that says the allocation could
happen somewhere else before this and remove the check if (!brd->channels[i]).
Also, remove all checks where ->channels[i] is checked if it's NULL or not
because if probe fails then that means that this scenario isn't possible (except
in the ioctl).

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 | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/drivers/staging/dgnc/dgnc_tty.c b/drivers/staging/dgnc/dgnc_tty.c
index ce4187f..0e48f95 100644
--- a/drivers/staging/dgnc/dgnc_tty.c
+++ b/drivers/staging/dgnc/dgnc_tty.c
@@ -304,19 +304,15 @@ int dgnc_tty_init(struct dgnc_board *brd)
 
 	brd->nasync = brd->maxports;
 
-	/*
-	 * Allocate channel memory that might not have been allocated
-	 * when the driver was first loaded.
-	 */
 	for (i = 0; i < brd->nasync; i++) {
-		if (!brd->channels[i]) {
-
-			/*
-			 * Okay to malloc with GFP_KERNEL, we are not at
-			 * interrupt context, and there are no locks held.
-			 */
-			brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
-		}
+		/*
+		 * Okay to malloc with GFP_KERNEL, we are not at
+		 * 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;
 	}
 
 	ch = brd->channels[0];
@@ -324,10 +320,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 +367,13 @@ int dgnc_tty_init(struct dgnc_board *brd)
 	}
 
 	return 0;
+
+err_free_channels:
+	for (i = i - 1; i >= 0; --i) {
+		kfree(brd->channels[i]);
+		brd->channels[i] = NULL;
+	}
+	return -ENOMEM;
 }
 
 
-- 
2.3.5


WARNING: multiple messages have this Message-ID (diff)
From: "Giedrius Statkevičius" <giedrius.statkevicius@gmail.com>
To: lidza.louina@gmail.com, markh@compro.net
Cc: devel@driverdev.osuosl.org, gregkh@linuxfoundation.org,
	driverdev-devel@linuxdriverproject.org,
	linux-kernel@vger.kernel.org, sudipm.mukherjee@gmail.com,
	dan.carpenter@oracle.com
Subject: [PATCH v5 1/3] staging: dgnc: clean up allocation of ->channels[i]
Date: Fri, 10 Apr 2015 02:42:29 +0300	[thread overview]
Message-ID: <1428622951-31061-1-git-send-email-giedrius.statkevicius@gmail.com> (raw)
In-Reply-To: <1428423102-29138-1-git-send-email-giedrius.statkevicius@gmail.com>

Check if kzalloc fails in dgnc_tty_init() and if it does then free all
previously allocated ->channels[i] and set them to NULL. This makes the code
less error/bug prone because instead of needing programmers attention to add
checks everywhere we do that in one place. Also, remove a bogus comment and
check in the same loop because ->channels[i] isn't allocated anywhere else.
Finally, remove a unnecessary check if ->channels[i] is NULL in the next loop
because it can't be.

Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@gmail.com>
---
v5: no change

v4: Make this patch only for dgnc_tty.c and only for this thing. Split into 3
patches (1 patch fixes a bug reported by Dan Carpenter)

v3: Remove the wrong comment at dgnc_tty_init() that says the allocation could
happen somewhere else before this and remove the check if (!brd->channels[i]).
Also, remove all checks where ->channels[i] is checked if it's NULL or not
because if probe fails then that means that this scenario isn't possible (except
in the ioctl).

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 | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/drivers/staging/dgnc/dgnc_tty.c b/drivers/staging/dgnc/dgnc_tty.c
index ce4187f..0e48f95 100644
--- a/drivers/staging/dgnc/dgnc_tty.c
+++ b/drivers/staging/dgnc/dgnc_tty.c
@@ -304,19 +304,15 @@ int dgnc_tty_init(struct dgnc_board *brd)
 
 	brd->nasync = brd->maxports;
 
-	/*
-	 * Allocate channel memory that might not have been allocated
-	 * when the driver was first loaded.
-	 */
 	for (i = 0; i < brd->nasync; i++) {
-		if (!brd->channels[i]) {
-
-			/*
-			 * Okay to malloc with GFP_KERNEL, we are not at
-			 * interrupt context, and there are no locks held.
-			 */
-			brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
-		}
+		/*
+		 * Okay to malloc with GFP_KERNEL, we are not at
+		 * 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;
 	}
 
 	ch = brd->channels[0];
@@ -324,10 +320,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 +367,13 @@ int dgnc_tty_init(struct dgnc_board *brd)
 	}
 
 	return 0;
+
+err_free_channels:
+	for (i = i - 1; i >= 0; --i) {
+		kfree(brd->channels[i]);
+		brd->channels[i] = NULL;
+	}
+	return -ENOMEM;
 }
 
 
-- 
2.3.5

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

  reply	other threads:[~2015-04-09 23:44 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-07 10:26 [PATCH] staging: dgnc: check if kzalloc fails in dgnc_tty_init() Giedrius Statkevičius
2015-04-07 10:41 ` Sudip Mukherjee
2015-04-07 11:53   ` Giedrius Statkevičius
2015-04-07 12:40 ` [PATCH v2] " Giedrius Statkevičius
2015-04-07 13:01   ` Dan Carpenter
2015-04-07 13:25     ` Giedrius Statkevičius
2015-04-07 14:11   ` [PATCH v3] " Giedrius Statkevičius
2015-04-07 14:24     ` Sudip Mukherjee
2015-04-07 14:37       ` Giedrius Statkevičius
2015-04-07 14:54         ` Dan Carpenter
2015-04-07 14:48     ` Dan Carpenter
2015-04-07 15:35       ` Giedrius Statkevičius
2015-04-07 15:46         ` Dan Carpenter
2015-04-07 15:46           ` Dan Carpenter
2015-04-07 16:11     ` [PATCH v4 1/3] staging: dgnc: clean up allocation of ->channels[i] Giedrius Statkevičius
2015-04-07 16:11       ` Giedrius Statkevičius
2015-04-09 23:42       ` Giedrius Statkevičius [this message]
2015-04-09 23:42         ` [PATCH v5 " Giedrius Statkevičius
2015-04-09 23:42       ` [PATCH v5 2/3] staging: dgnc: don't forget to check if ->channels[i] is NULL in dgnc_tty_uninit() Giedrius Statkevičius
2015-04-09 23:42         ` Giedrius Statkevičius
2015-04-09 23:42       ` [PATCH v5 3/3] staging: dgnc: remove redundant !ch checks Giedrius Statkevičius
2015-04-09 23:42         ` Giedrius Statkevičius
2015-04-07 16:11     ` [PATCH v4 2/3] staging: dgnc: don't forget to check if ->channels[i] is NULL in dgnc_tty_uninit() Giedrius Statkevičius
2015-04-07 16:11       ` Giedrius Statkevičius
2015-04-07 16:11     ` [PATCH v4 3/3] staging: dgnc: remove redundant !ch checks Giedrius Statkevičius
2015-04-07 16:11       ` Giedrius Statkevičius
2015-04-10  0:37       ` Dan Carpenter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1428622951-31061-1-git-send-email-giedrius.statkevicius@gmail.com \
    --to=giedrius.statkevicius@gmail.com \
    --cc=dan.carpenter@oracle.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=driverdev-devel@linuxdriverproject.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=lidza.louina@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markh@compro.net \
    --cc=sudipm.mukherjee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.