wifi: libertas: fix some memleaks in lbs_allocate_cmd_buffer()
authorZhipeng Lu <alexious@zju.edu.cn>
Fri, 26 Jan 2024 07:53:34 +0000 (15:53 +0800)
committerKalle Valo <kvalo@kernel.org>
Mon, 5 Feb 2024 18:15:27 +0000 (20:15 +0200)
In the for statement of lbs_allocate_cmd_buffer(), if the allocation of
cmdarray[i].cmdbuf fails, both cmdarray and cmdarray[i].cmdbuf needs to
be freed. Otherwise, there will be memleaks in lbs_allocate_cmd_buffer().

Fixes: 876c9d3aeb98 ("[PATCH] Marvell Libertas 8388 802.11b/g USB driver")
Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
Signed-off-by: Kalle Valo <kvalo@kernel.org>
Link: https://msgid.link/20240126075336.2825608-1-alexious@zju.edu.cn
drivers/net/wireless/marvell/libertas/cmd.c

index 104d2b6dc9af6ec2add89b5c32e08c835033ebc6..5a525da434c28192784fae0101acd1b46100858c 100644 (file)
@@ -1132,7 +1132,7 @@ int lbs_allocate_cmd_buffer(struct lbs_private *priv)
                if (!cmdarray[i].cmdbuf) {
                        lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
                        ret = -1;
-                       goto done;
+                       goto free_cmd_array;
                }
        }
 
@@ -1140,8 +1140,17 @@ int lbs_allocate_cmd_buffer(struct lbs_private *priv)
                init_waitqueue_head(&cmdarray[i].cmdwait_q);
                lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
        }
-       ret = 0;
+       return 0;
 
+free_cmd_array:
+       for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
+               if (cmdarray[i].cmdbuf) {
+                       kfree(cmdarray[i].cmdbuf);
+                       cmdarray[i].cmdbuf = NULL;
+               }
+       }
+       kfree(priv->cmd_array);
+       priv->cmd_array = NULL;
 done:
        return ret;
 }