Skip to contentSkip to conversation

kernel/resource.c: fix memory leak

PATCH NAK Alexander Beregalov 3 participants 3 messages Jan 9, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/resource.c: fix memory leak

Free res before exit.
Found by cppcheck.

---
 kernel/resource.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/resource.c +2 -1
index af96c1e..ae2e177 100644
@@ -545,11 +545,12 @@ static void __init __reserve_region_with_split(struct resource *root,
 	res->flags = IORESOURCE_BUSY;
 
 	conflict = __request_resource(parent, res);
+	kfree(res);
+
 	if (!conflict)
 		return;
 
 	/* failed, split and try again */
-	kfree(res);
 
 	/* conflict covered whole area */
 	if (conflict->start <= start && conflict->end >= end)
Signature · 1 line
1.6.6
#1 lore raw
Quoted 16 lines · Alexander Beregalov

Free res before exit.
Found by cppcheck.

---
kernel/resource.c | 3 ++-

1 files changed, 2 insertions(+), 1 deletions(-)
kernel/resource.c +0 -0
index af96c1e..ae2e177 100644
@@ -545,11 +545,12 @@ static void __init __reserve_region_with_split(struct resource *root,
 	res->flags = IORESOURCE_BUSY;
 
 	conflict = __request_resource(parent, res);

kernel/resource.c static void __init __reserve_region_with_split(struct resource *root, · line 556

Quoted 10 lines
kernel/resource.c +2 -1
+	kfree(res);
+
 	if (!conflict)
 		return;
 
 	/* failed, split and try again */
-	kfree(res);
 
 	/* conflict covered whole area */
 	if (conflict->start <= start && conflict->end >= end)

NAK.

If I read it correctly, 'res' will be used if !conflict, we should't kfree it.

Signature · 1 line
Live like a child, think like the god.
#2 lore raw
In reply to Alexander Beregalov
Quoted 14 lines · Alexander Beregalov

Free res before exit.
Found by cppcheck.

---
 kernel/resource.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
kernel/resource.c +0 -0
index af96c1e..ae2e177 100644

kernel/resource.c static void __init __reserve_region_with_split(struct resource *root, · line 552

Quoted 10 lines
kernel/resource.c +2 -1
@@ -545,11 +545,12 @@ static void __init __reserve_region_with_split(struct resource *root,
 	res->flags = IORESOURCE_BUSY;
 
 	conflict = __request_resource(parent, res);
+	kfree(res);
+
 	if (!conflict)
 		return;
 
 	/* failed, split and try again */
-	kfree(res);

As Amerigo pointed out, this is very wrong indeed.

If we don't have a conflict, then __request_resource() will have inserted the resource into the resource tree, and we absolutely must _not_ free it.

So that cppcheck tool is way too simplistic, and wrong.

Linus