public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] md: Combine two kmalloc() calls into one in sb_equal()
@ 2016-12-09 18:30 SF Markus Elfring
  2016-12-09 19:05 ` Joe Perches
  2016-12-09 19:09 ` Bernd Schubert
  0 siblings, 2 replies; 11+ messages in thread
From: SF Markus Elfring @ 2016-12-09 18:30 UTC (permalink / raw)
  To: linux-raid, Shaohua Li; +Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 9 Dec 2016 19:09:13 +0100

The function "kmalloc" was called in one case by the function "sb_equal"
without checking immediately if it failed.
This issue was detected by using the Coccinelle software.

Perform the desired memory allocation (and release at the end)
by a single function call instead.

Fixes: 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 ("Linux-2.6.12-rc2")

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/md/md.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index b088668269b0..86caf2536255 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -843,15 +843,12 @@ static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
 	int ret;
 	mdp_super_t *tmp1, *tmp2;
 
-	tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
-	tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
-
-	if (!tmp1 || !tmp2) {
-		ret = 0;
-		goto abort;
-	}
+	tmp1 = kmalloc(2 * sizeof(*tmp1), GFP_KERNEL);
+	if (!tmp1)
+		return 0;
 
 	*tmp1 = *sb1;
+	tmp2 = tmp1 + 1;
 	*tmp2 = *sb2;
 
 	/*
@@ -861,9 +858,7 @@ static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
 	tmp2->nr_disks = 0;
 
 	ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
-abort:
 	kfree(tmp1);
-	kfree(tmp2);
 	return ret;
 }
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2016-12-09 22:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-09 18:30 [PATCH] md: Combine two kmalloc() calls into one in sb_equal() SF Markus Elfring
2016-12-09 19:05 ` Joe Perches
2016-12-09 20:05   ` SF Markus Elfring
2016-12-09 20:51     ` Joe Perches
2016-12-09 21:30   ` [PATCH] " Al Viro
2016-12-09 21:57     ` Joe Perches
2016-12-09 19:09 ` Bernd Schubert
2016-12-09 19:54   ` SF Markus Elfring
2016-12-09 21:18     ` Bernd Schubert
2016-12-09 21:58       ` SF Markus Elfring
2016-12-09 22:04         ` Bernd Schubert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox