aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Jenkins <alan-jenkins@tuffmail.co.uk>2009-09-30 17:22:10 +0100
committerAlan Jenkins <alan-jenkins@tuffmail.co.uk>2010-02-25 16:00:44 +0000
commit8c7c5c102426cafcb2d118e307f246df66d650de (patch)
treea3ae13a56480d0fd68c3c8f56ed2816457a6d3b7
parent6392a1a21c0c2a6edb6579f27d897b5c880ad98d (diff)
downloadmodule-init-tools-alan-cleanups.tar.gz
insmod: fix memory leakalan-cleanups
insmod makes a half-successful attempt to clean up on failure. Let's make it clean up on all failures, and remember to clean up on success as well. This leaves modprobe as the only leaky program, at least for the code paths executed by the test suite. Signed-off-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk>
-rw-r--r--insmod.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/insmod.c b/insmod.c
index 3a2a910..de4f68b 100644
--- a/insmod.c
+++ b/insmod.c
@@ -60,9 +60,7 @@ static void *grab_file(const char *filename, unsigned long *size)
{
unsigned int max = 16384;
int ret, fd, err_save;
- void *buffer = malloc(max);
- if (!buffer)
- return NULL;
+ void *buffer;
if (streq(filename, "-"))
fd = dup(STDIN_FILENO);
@@ -72,6 +70,10 @@ static void *grab_file(const char *filename, unsigned long *size)
if (fd < 0)
return NULL;
+ buffer = malloc(max);
+ if (!buffer)
+ goto out_error;
+
*size = 0;
while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
*size += ret;
@@ -158,7 +160,10 @@ int main(int argc, char *argv[])
if (ret != 0) {
fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
filename, ret, moderror(errno));
- exit(1);
}
+ free(file);
+
+ if (ret != 0)
+ exit(1);
exit(0);
}