diff options
-rw-r--r-- | flash.c | 29 |
1 files changed, 26 insertions, 3 deletions
@@ -29,6 +29,10 @@ #define PERSIST_ERR_COULDNTWRITE 9 #define PERSIST_ERR_COUNDNTOPENFILE 10 +// safety check toggles +#define FROZENSMALLEROK 1 +#define FROZENBIGGEROK 0 + // flash writing queue bits #define MAXQUEUEDJOBS 1 // This makes the queue a little bit pointless // but you might want to queue a few more @@ -163,7 +167,7 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in // load the data persistence_printheader(&hdr); - if (read(fd, payload, total) != total) { + if (read(fd, payload, hdr.length) != hdr.length) { errno = PERSIST_ERR_COULDNTREADDATA; free(payload); close(fd); @@ -244,10 +248,26 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v // check that the length of this frozen object is what we are expecting if (hdr.length != len) { + +#if FROZENSMALLEROK + if (hdr.length < len) { + printf("frozen struct is %d bytes smaller than the requested size\n", len - hdr.length); + goto hdrlengthok; + } +#endif + +#if FROZENBIGGEROK + if(hdr.length > len) { + printf("frozen struct is bigger than the requested size, %d bytes will be truncated\n", hdr.length - len); + goto hdrlengthok; + } +#endif + errno = PERSIST_ERR_HDRLENMISMATCH; close(fd); return false; } +hdrlengthok: // check that it's the same version.. the version isn't used at the moment // but if you want to change the header at some point it'll be useful @@ -259,14 +279,15 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v // read in the data for the object.. if we couldn't read the amount of data // that the header said there was the header is either wrong or the file is truncated. - if (read(fd, result, hdr.length) != hdr.length) { + char* tempresult = g_malloc(hdr.length); + if (read(fd, tempresult, hdr.length) != hdr.length) { errno = PERSIST_ERR_COULDNTREADDATA; close(fd); return false; } // check it's crc32 to make sure it's not corrupt - uint32_t calculatedcrc32 = crc32(result, hdr.length); + uint32_t calculatedcrc32 = crc32(tempresult, hdr.length); if (calculatedcrc32 != hdr.crc32) { printf("Calculated CRC is 0x%08"PRIx32"\n", calculatedcrc32); errno = PERSIST_ERR_BADCRC32; @@ -275,6 +296,8 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v } close(fd); + memcpy(result, tempresult, MIN(len, hdr.length)); + g_free(tempresult); return true; } |