[PATCH] nbd: fix possible memory leak in nbd_add_socket()
If kzalloc() fails, the memory allocated for the new nbd_sock by krealloc()
will not be used until the next nbd_add_socket() call.
That's a memory leak.
We can fix it by adjusting the order.
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
drivers/block/nbd.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 08696f5..a197109 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -944,14 +944,16 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
return -EBUSY;
}
- socks = krealloc(config->socks, (config->num_connections + 1) *
- sizeof(struct nbd_sock *), GFP_KERNEL);
- if (!socks) {
+ nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
+ if (!nsock) {
sockfd_put(sock);
return -ENOMEM;
}
- nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
- if (!nsock) {
+
+ socks = krealloc(config->socks, (config->num_connections + 1) *
+ sizeof(struct nbd_sock *), GFP_KERNEL);
+ if (!socks) {
+ kfree(nsock);
sockfd_put(sock);
return -ENOMEM;
}
--
1.8.3.1
Reply to: