Skip to contentSkip to conversation

bcache: Check for NULL return of kzalloc()

PATCH In review QintaoShen 2 participants 2 messages Mar 24, 2022 – Apr 7, 2022 bcache (block layer cache)

lore
Share & tools
lore

QintaoShen

author
#0 lore raw

PATCH 1/1 bcache: Check for NULL return of kzalloc()

kzalloc() is a memory allocation function which may return a NULL pointer. Therefore, it is better to check the return value of kzalloc() to avoid potential NULL-pointer dereference.

---
 drivers/md/bcache/request.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/md/bcache/request.c +3 -0
index fdd0194..232ffe3 100644
@@ -1105,6 +1105,9 @@ static void detached_dev_do_request(struct bcache_device *d, struct bio *bio,
 	 * which would call closure_get(&dc->disk.cl)
 	 */
 	ddip = kzalloc(sizeof(struct detached_dev_io_private), GFP_NOIO);
+	if (!ddpp)
+		return ;
+
 	ddip->d = d;
 	/* Count on the bcache device */
 	ddip->orig_bdev = orig_bdev;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 14 lines · QintaoShen

kzalloc() is a memory allocation function which may return a NULL pointer. Therefore, it is better to check the return value of kzalloc() to avoid potential NULL-pointer dereference.

---
  drivers/md/bcache/request.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/md/bcache/request.c +0 -0
index fdd0194..232ffe3 100644

drivers/md/bcache/request.c static void detached_dev_do_request(struct bcache_device *d, struct bio *bio, · line 1109

Quoted 5 lines
drivers/md/bcache/request.c +2 -0
@@ -1105,6 +1105,9 @@ static void detached_dev_do_request(struct bcache_device *d, struct bio *bio,
  	 * which would call closure_get(&dc->disk.cl)
  	 */
  	ddip = kzalloc(sizeof(struct detached_dev_io_private), GFP_NOIO);
+	if (!ddpp)
+		return ;

This is wrong. If you test your patch, you may experience a system hang I guess.

Coly Li

Quoted 4 lines
drivers/md/bcache/request.c +1 -0
+
  	ddip->d = d;
  	/* Count on the bcache device */
  	ddip->orig_bdev = orig_bdev;