Skip to contentSkip to conversation

staging: greybus: loopback_test: fix device-name leak

PATCH Superseded Gargi Sharma 3 participants 6 messages Feb 21, 2017 – Feb 22, 2017

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: greybus: loopback_test: fix device-name leak

Change array index from the loop bound variable to loop index. Memory leak was occuring whenever there was more than one loopback device. The pointer to dirent structures must be individually freed before freeing the pointer array.

Coccinelle Script:
@@
expression arr,ex1,ex2;
@@

for(ex1 = 0; ex1 < ex2; ex1++) { <...
arr[
- ex2
+ ex1
]
...> }

---
Changes in v2:
-Made the commit message clearer.

---
 drivers/staging/greybus/tools/loopback_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/greybus/tools/loopback_test.c +1 -1
index 18d7a3d..1c01833 100644
@@ -636,7 +636,7 @@ int find_loopback_devices(struct loopback_test *t)
 	ret = 0;
 done:
 	for (i = 0; i < n; i++)
-		free(namelist[n]);
+		free(namelist[i]);
 	free(namelist);
 baddir:
 	return ret;
Signature · 1 line
2.7.4
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 17 lines · Gargi Sharma

Change array index from the loop bound variable to loop index. Memory leak was occuring whenever there was more than one loopback device. The pointer to dirent structures must be individually freed before freeing the pointer array.

Coccinelle Script:
@@
expression arr,ex1,ex2;
@@

for(ex1 = 0; ex1 < ex2; ex1++) { <...
arr[
- ex2
+ ex1
]
...> }

Quoted 4 lines

---
Changes in v2:
-Made the commit message clearer.

Thanks,
Johan

maintainer
#2 lore raw
In reply to Gargi Sharma
Quoted 30 lines · Gargi Sharma

Change array index from the loop bound variable to loop index. Memory leak was occuring whenever there was more than one loopback device. The pointer to dirent structures must be individually freed before freeing the pointer array.

Coccinelle Script:
@@
expression arr,ex1,ex2;
@@

for(ex1 = 0; ex1 < ex2; ex1++) { <...
arr[
- ex2
+ ex1
]
...> }

---
Changes in v2:
-Made the commit message clearer.

---
 drivers/staging/greybus/tools/loopback_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/greybus/tools/loopback_test.c +0 -0
index 18d7a3d..1c01833 100644

drivers/staging/greybus/tools/loopback_test.c int find_loopback_devices(struct loopback_test *t) · line 642

Quoted 8 lines
drivers/staging/greybus/tools/loopback_test.c +1 -1
@@ -636,7 +636,7 @@ int find_loopback_devices(struct loopback_test *t)
 	ret = 0;
 done:
 	for (i = 0; i < n; i++)
-		free(namelist[n]);
+		free(namelist[i]);
 	free(namelist);
 baddir:
 	return ret;

Oh, there's an illegal free here, which probably was what Julia was referring to. Yeah, not sure why things haven't blown up, perhaps there happens to be NULL pointer after?

So we are in fact always leaking the device name here.

No need to resend, unless you prefer. You can add my Reviewed-by tag if so.

Thanks,
Johan

#3 lore raw
Quoted 27 lines · Johan Hovold
Quoted 25 lines · Gargi Sharma

Change array index from the loop bound variable to loop index. Memory leak was occuring whenever there was more than one loopback device. The pointer to dirent structures must be individually freed before freeing the pointer array.

Coccinelle Script:
@@
expression arr,ex1,ex2;
@@

for(ex1 = 0; ex1 < ex2; ex1++) { <...
arr[
- ex2
+ ex1
]
...> }

---
Changes in v2:
-Made the commit message clearer.

---
 drivers/staging/greybus/tools/loopback_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/staging/greybus/tools/loopback_test.c

Quoted 19 lines

drivers/staging/greybus/tools/loopback_test.c int find_loopback_devices(struct loopback_test *t) · line 642

Quoted 12 lines

index 18d7a3d..1c01833 100644

drivers/staging/greybus/tools/loopback_test.c +1 -1
@@ -636,7 +636,7 @@ int find_loopback_devices(struct loopback_test *t)
      ret = 0;
 done:
      for (i = 0; i < n; i++)
-             free(namelist[n]);
+             free(namelist[i]);
      free(namelist);
 baddir:
      return ret;

Oh, there's an illegal free here, which probably was what Julia was referring to. Yeah, not sure why things haven't blown up, perhaps there happens to be NULL pointer after?

Quoted 2 lines

So we are in fact always leaking the device name here.

Um, should I assign null pointers to device names before freeing them? That should fix illegal free I believe.

Quoted 2 lines

No need to resend, unless you prefer. You can add my Reviewed-by tag if so.

Not sure what this means. I can send a v3 with the above suggested changes if they are correct.

Thanks,
Gargi

Quoted 5 lines

Thanks,
Johan

--

#4 lore raw
Quoted 48 lines · Gargi Sharma
Quoted 27 lines · Johan Hovold
Quoted 25 lines · Gargi Sharma

Change array index from the loop bound variable to loop index. Memory leak was occuring whenever there was more than one loopback device. The pointer to dirent structures must be individually freed before freeing the pointer array.

Coccinelle Script:
@@
expression arr,ex1,ex2;
@@

for(ex1 = 0; ex1 < ex2; ex1++) { <...
  arr[
- ex2
+ ex1
  ]
  ...> }

---
Changes in v2:
      -Made the commit message clearer.
---
 drivers/staging/greybus/tools/loopback_test.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/staging/greybus/tools/loopback_test.c

Quoted 18 lines
Quoted 5 lines

index 18d7a3d..1c01833 100644

drivers/staging/greybus/tools/loopback_test.c +0 -0
@@ -636,7 +636,7 @@ int find_loopback_devices(struct loopback_test *t)
       ret = 0;
Quoted 7 lines

 done:
      for (i = 0; i < n; i++)
-             free(namelist[n]);
+             free(namelist[i]);
      free(namelist);
 baddir:
      return ret;

Oh, there's an illegal free here, which probably was what Julia was referring to. Yeah, not sure why things haven't blown up, perhaps there happens to be NULL pointer after?

Quoted 5 lines
Quoted 2 lines

So we are in fact always leaking the device name here.

Um, should I assign null pointers to device names before freeing them? That should fix illegal free I believe.

No, your change eliminates the illegal free.

scandir is returning an array of n elements. In such an array, the elements have numbers 0 .. n-1. namelist[n] doesn't exist. If one is very lucky, there is a NULL after the end of the array, and then the free does nothing. But doing nothing is not the goal here. The goal is to free the elements 0 .. n-1. They have all been allocated, according to how scandir works, so it will be fine to free them.

julia

Quoted 25 lines
Quoted 3 lines

No need to resend, unless you prefer. You can add my Reviewed-by tag if so.

Not sure what this means. I can send a v3 with the above suggested changes if they are correct.

Thanks,
Gargi

Quoted 5 lines

Thanks,
Johan

--

--
You received this message because you are subscribed to the Google Groups
"outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/CAOCi2DHkHpakjYezcnCXF35 ViB5%2BLW174r_%3DAYMCt22OYg3X5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

maintainer
#5 lore raw
In reply to Gargi Sharma
Quoted 47 lines · Gargi Sharma
Quoted 27 lines · Johan Hovold
Quoted 25 lines · Gargi Sharma

Change array index from the loop bound variable to loop index. Memory leak was occuring whenever there was more than one loopback device. The pointer to dirent structures must be individually freed before freeing the pointer array.

Coccinelle Script:
@@
expression arr,ex1,ex2;
@@

for(ex1 = 0; ex1 < ex2; ex1++) { <...
arr[
- ex2
+ ex1
]
...> }

---
Changes in v2:
-Made the commit message clearer.

---
 drivers/staging/greybus/tools/loopback_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/staging/greybus/tools/loopback_test.c

Quoted 17 lines

drivers/staging/greybus/tools/loopback_test.c int find_loopback_devices(struct loopback_test *t) · line 642

Quoted 12 lines

index 18d7a3d..1c01833 100644

drivers/staging/greybus/tools/loopback_test.c +1 -1
@@ -636,7 +636,7 @@ int find_loopback_devices(struct loopback_test *t)
      ret = 0;
 done:
      for (i = 0; i < n; i++)
-             free(namelist[n]);
+             free(namelist[i]);
      free(namelist);
 baddir:
      return ret;

Oh, there's an illegal free here, which probably was what Julia was referring to. Yeah, not sure why things haven't blown up, perhaps there happens to be NULL pointer after?

Quoted 4 lines
Quoted 1 line

So we are in fact always leaking the device name here.

Um, should I assign null pointers to device names before freeing them? That should fix illegal free I believe.

No, your fix is fine as is, it's just a matter of describing the problem accurately in the commit message.

Quoted 4 lines
Quoted 2 lines

No need to resend, unless you prefer. You can add my Reviewed-by tag if so.

Not sure what this means. I can send a v3 with the above suggested changes if they are correct.

Sorry if I was being unclear. I meant to say that your current commit message is good enough, but perhaps it is indeed better if you do send a v3 where you mention that we have been leaking all the device names always (and not just when there have been more than one as I incorrectly suggested).

You can also mention that you fix that illegal free of namelist[n], which must so far have happened to be NULL (I verified that it is indeed NULL here on my system too).

Thanks,
Johan