Skip to contentSkip to conversation

cifsd: fix memory leak when loop ends

PATCH In review Muhammad Usama Anjum 2 participants 2 messages Apr 1, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cifsd: fix memory leak when loop ends

Memory is being allocated and if veto_list is zero, the loop breaks without cleaning up the allocated memory. In this patch, the length check has been moved before allocation. If loop breaks, the memory isn't allocated in the first place. Thus the memory is being protected from leaking.

---
 fs/cifsd/mgmt/share_config.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/cifsd/mgmt/share_config.c +4 -4
index db780febd692..b2bd789af945 100644
@@ -92,14 +92,14 @@ static int parse_veto_list(struct ksmbd_share_config *share,
 	while (veto_list_sz > 0) {
 		struct ksmbd_veto_pattern *p;
 
-		p = kzalloc(sizeof(struct ksmbd_veto_pattern), GFP_KERNEL);
-		if (!p)
-			return -ENOMEM;
-
 		sz = strlen(veto_list);
 		if (!sz)
 			break;
 
+		p = kzalloc(sizeof(struct ksmbd_veto_pattern), GFP_KERNEL);
+		if (!p)
+			return -ENOMEM;
+
 		p->pattern = kstrdup(veto_list, GFP_KERNEL);
 		if (!p->pattern) {
 			ksmbd_free(p);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines

Memory is being allocated and if veto_list is zero, the loop breaks without cleaning up the allocated memory. In this patch, the length check has been moved before allocation. If loop breaks, the memory isn't allocated in the first place. Thus the memory is being protected from leaking.

I will apply. Thanks for your patch!