mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: SF Markus Elfring <elfring@users.sourceforge.net>
To: Maxim Levitsky <maximlevitsky@gmail.com>,
	kernel-janitors@vger.kernel.org
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH 1/9] memstick: Split a condition check in msb_ftl_initialize()
Date: Sat, 7 Jan 2017 20:53:02 +0100	[thread overview]
Message-ID: <b30a04c0-fea7-c5d6-a050-299f01212437@users.sourceforge.net> (raw)
In-Reply-To: <64dfa775-4e4a-bdb0-7b55-41f43ccdf11a@users.sourceforge.net>

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sat, 7 Jan 2017 08:43:50 +0100

The functions "kmalloc" and "kzalloc" were called in two cases by the
function "msb_ftl_initialize" without checking immediately
if they succeded.
This issue was detected by using the Coccinelle software.

Split a condition check for memory allocation failures so that
the corresponding exception handling will be improved a bit.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/memstick/core/ms_block.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
index f3512404bc52..fd8b1697a5a2 100644
--- a/drivers/memstick/core/ms_block.c
+++ b/drivers/memstick/core/ms_block.c
@@ -1339,17 +1339,17 @@ static int msb_ftl_initialize(struct msb_data *msb)
 	msb->logical_block_count = msb->zone_count * 496 - 2;
 
 	msb->used_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
+	if (!msb->used_blocks_bitmap)
+		return -ENOMEM;
+
 	msb->erased_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
+	if (!msb->erased_blocks_bitmap)
+		goto free_used_bitmap;
+
 	msb->lba_to_pba_table =
 		kmalloc(msb->logical_block_count * sizeof(u16), GFP_KERNEL);
-
-	if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
-						!msb->erased_blocks_bitmap) {
-		kfree(msb->used_blocks_bitmap);
-		kfree(msb->lba_to_pba_table);
-		kfree(msb->erased_blocks_bitmap);
-		return -ENOMEM;
-	}
+	if (!msb->lba_to_pba_table)
+		goto free_erased_bitmap;
 
 	for (i = 0; i < msb->zone_count; i++)
 		msb->free_block_count[i] = MS_BLOCKS_IN_ZONE;
@@ -1362,6 +1362,11 @@ static int msb_ftl_initialize(struct msb_data *msb)
 
 	msb->ftl_initialized = true;
 	return 0;
+free_erased_bitmap:
+	kfree(msb->erased_blocks_bitmap);
+free_used_bitmap:
+	kfree(msb->used_blocks_bitmap);
+	return -ENOMEM;
 }
 
 static int msb_ftl_scan(struct msb_data *msb)
-- 
2.11.0

  reply	other threads:[~2017-01-07 19:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-07 19:50 [PATCH 0/9] memstick: Fine-tuning for some function implementations SF Markus Elfring
2017-01-07 19:53 ` SF Markus Elfring [this message]
2017-01-07 19:54 ` [PATCH 2/9] memstick: Use kmalloc_array() in msb_ftl_initialize() SF Markus Elfring
2017-01-07 19:55 ` [PATCH 3/9] memstick: Delete unwanted spaces behind three function parameters SF Markus Elfring
2017-01-07 19:56 ` [PATCH 4/9] memstick: Improve two size determinations in msb_resume() SF Markus Elfring
2017-01-07 19:57 ` [PATCH 5/9] memstick: Delete an unnecessary variable initialisation " SF Markus Elfring
2017-01-07 19:58 ` [PATCH 6/9] memstick: Improve a size determination in msb_probe() SF Markus Elfring
2017-01-07 19:59 ` [PATCH 7/9] memstick: Improve another size determination in msb_read_boot_blocks() Markus Elfring
2017-01-07 20:01 ` SF Markus Elfring
2017-01-07 20:03 ` [PATCH 8/9] memstick: Delete an unnecessary variable initialisation " SF Markus Elfring
2017-01-07 20:04 ` [PATCH 9/9] memstick: Delete an unnecessary check in msb_update_block() SF Markus Elfring

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=b30a04c0-fea7-c5d6-a050-299f01212437@users.sourceforge.net \
    --to=elfring@users.sourceforge.net \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maximlevitsky@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome