Skip to contentSkip to conversation

lib/test_kasan.c: Fix memory leak in kmalloc_oob_krealloc_more()

PATCH Applied Gustavo A. R. Silva 2 participants 2 messages Jan 23, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/test_kasan.c: Fix memory leak in kmalloc_oob_krealloc_more()

In case memory resources for _ptr2_ were allocated, release them before return.

Notice that in case _ptr1_ happens to be NULL, krealloc() behaves exactly like kmalloc().

Addresses-Coverity-ID: 1490594 ("Resource leak")

Fixes 3f15801cdc23 ("lib: add kasan test module")
---
 lib/test_kasan.c | 1 +
 1 file changed, 1 insertion(+)
lib/test_kasan.c +1 -0
index 328d33beae36..3872d250ed2c 100644
@@ -158,6 +158,7 @@ static noinline void __init kmalloc_oob_krealloc_more(void)
 	if (!ptr1 || !ptr2) {
 		pr_err("Allocation failed\n");
 		kfree(ptr1);
+		kfree(ptr2);
 		return;
 	}
 
Signature · 1 line
2.25.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Gustavo A. R. Silva

In case memory resources for _ptr2_ were allocated, release them before return.

Notice that in case _ptr1_ happens to be NULL, krealloc() behaves exactly like kmalloc().

The scenario where ptr1 is NULL, but ptr2 is not NULL is not impossible indeed.

Quoted 23 lines

Addresses-Coverity-ID: 1490594 ("Resource leak")

Fixes 3f15801cdc23 ("lib: add kasan test module")
---
 lib/test_kasan.c | 1 +
 1 file changed, 1 insertion(+)
lib/test_kasan.c +1 -1
index 328d33beae36..3872d250ed2c 100644
@@ -158,6 +158,7 @@ static noinline void __init kmalloc_oob_krealloc_more(void)
        if (!ptr1 || !ptr2) {
                pr_err("Allocation failed\n");
                kfree(ptr1);
+               kfree(ptr2);
                return;
        }
 
--

2.25.0