summaryrefslogtreecommitdiff
path: root/lib/bootstrap
diff options
context:
space:
mode:
authorAndrey Smirnov <andrew.smirnov@gmail.com>2015-05-06 12:32:10 -0700
committerSascha Hauer <s.hauer@pengutronix.de>2015-05-07 09:49:41 +0200
commit065d75c341ed473f51c7f7e486f09419387d9894 (patch)
tree03cae8e359ab306859db18392e53f39f39e5baf7 /lib/bootstrap
parentd26ed41facc4c18d33730ad935531aebc0d1fa71 (diff)
downloadbarebox-065d75c341ed473f51c7f7e486f09419387d9894.tar.gz
bootstrap_read_devfs(): Fix potential memory leak
In case of a failure in one of the cdev_* functions original version of bootstrap_read_devfs would not release memory allocated for barebox header or memory allocated for the image. This commit fixes this by adding resource deallocation code. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib/bootstrap')
-rw-r--r--lib/bootstrap/devfs.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index 5693431a9b..576f15159e 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -81,7 +81,7 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
{
int ret;
int size = 0;
- void *to, *header;
+ void *to, *header, *result = NULL;
struct cdev *cdev, *partition;
char *partname = "x";
@@ -116,16 +116,21 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
cdev = cdev_open(partname, O_RDONLY);
if (!cdev) {
bootstrap_err("%s: failed to open %s\n", devname, partname);
- return NULL;
+ goto free_memory;
}
ret = cdev_read(cdev, to, size, 0, 0);
cdev_close(cdev);
- if (ret != size) {
+ if (ret != size)
bootstrap_err("%s: failed to read from %s\n", devname, partname);
- return NULL;
- }
+ else
+ result = to;
+
+free_memory:
+ free(header);
+ if (!result)
+ free(to);
- return to;
+ return result;
}