diff options
author | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-22 14:43:20 -0400 |
---|---|---|
committer | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-22 14:43:20 -0400 |
commit | f4f3abc9549d038d4d1d9a58d06471ce5435eb25 (patch) | |
tree | 99da029e6c394df8ceeb77f575b4cae974bf7845 /flash.c | |
parent | cf7bc9aa99868c38ebb3fe032485e0513f493526 (diff) |
fix memory leaks
Diffstat (limited to 'flash.c')
-rw-r--r-- | flash.c | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -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; } |