summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael J. Chudobiak <mjc@avtechpulse.com>2012-08-22 14:43:20 -0400
committerMichael J. Chudobiak <mjc@avtechpulse.com>2012-08-22 14:43:20 -0400
commitf4f3abc9549d038d4d1d9a58d06471ce5435eb25 (patch)
tree99da029e6c394df8ceeb77f575b4cae974bf7845
parentcf7bc9aa99868c38ebb3fe032485e0513f493526 (diff)
fix memory leaks
-rw-r--r--bus.c2
-rw-r--r--flash.c12
2 files changed, 11 insertions, 3 deletions
diff --git a/bus.c b/bus.c
index 26963cc..2bebb57 100644
--- a/bus.c
+++ b/bus.c
@@ -274,7 +274,7 @@ static int gpio_readvalue(unsigned base, unsigned io)
gpio_getvaluenodepath(base, io, path);
FILE* valuefile = fopen(path, "r");
if (valuefile != NULL) {
- char value[2]; // value will be a single char and \n
+ char value[2]; // value will be a single char and \n
fread(value, 1, 2, valuefile);
fclose(valuefile);
return atoi(value);
diff --git a/flash.c b/flash.c
index 9bc9e9d..62565d3 100644
--- a/flash.c
+++ b/flash.c
@@ -1,6 +1,4 @@
#include "globals.h"
-#include "lcd.h"
-#include "version.h"
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
@@ -118,6 +116,7 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
// create a buffer for the existing data
void* payload = malloc(total);
if (payload == NULL) {
+ close(fd);
errno = PERSIST_ERR_COULDNTALLOCATEBUFFER;
return false;
}
@@ -126,6 +125,8 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
persistencehdr hdr;
if (read(fd, &hdr, sizeof(persistencehdr)) != sizeof(persistencehdr)) {
errno = PERSIST_ERR_COULDNTREADHDR;
+ free(payload);
+ close(fd);
return false;
}
@@ -133,6 +134,8 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
persistence_printheader(&hdr);
if (read(fd, payload, total) != total) {
errno = PERSIST_ERR_COULDNTREADDATA;
+ free(payload);
+ close(fd);
return false;
}
@@ -140,6 +143,8 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
uint32_t calculatedcrc32 = crc32((uint8_t*) payload, hdr.length);
if (calculatedcrc32 != hdr.crc32) {
errno = PERSIST_ERR_BADCRC32;
+ free(payload);
+ close(fd);
return false;
}
// overlay the payload with the existing data
@@ -164,6 +169,7 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
// write the header
if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
errno = PERSIST_ERR_COULDNTWRITE;
+ close(fd);
return false;
}
@@ -171,12 +177,14 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
if (lseek(fd, offset, SEEK_CUR) < 0) {
errno = PERSIST_ERR_COULDNTSEEK; // shouldn't ever happen really because if we're actually
// seeking the file should already be the total size.
+ close(fd);
return false;
}
// write the data out to disk
if (write(fd, ((char*) data) + offset, len) != len) {
errno = PERSIST_ERR_COULDNTWRITE;
+ close(fd);
return false;
}