From e538fe77067750d99d0978ae7b74a88ab09f02ee Mon Sep 17 00:00:00 2001 From: DeadlySystem Date: Sat, 8 Mar 2014 22:58:45 +0100 Subject: [PATCH] Update unpack-pbp.c --- tools/unpack-pbp.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tools/unpack-pbp.c b/tools/unpack-pbp.c index 011a1959..fb2a43a2 100644 --- a/tools/unpack-pbp.c +++ b/tools/unpack-pbp.c @@ -149,7 +149,14 @@ int main(int argc, char *argv[]) { printf("ERROR: Could not open the output file. (%s)\n", filename[loop0]); return -1; } - + + // Create the read buffer + buffer = malloc(maxbuffer); + if (buffer == NULL) { + printf("ERROR: Could not allocate the section data buffer. (%d)\n", readsize); + return -1; + } + do { int readsize; @@ -161,31 +168,27 @@ int main(int argc, char *argv[]) { } size -= readsize; - // Create the read buffer - buffer = malloc(readsize); - if (buffer == NULL) { - printf("ERROR: Could not allocate the section data buffer. (%d)\n", readsize); - return -1; - } - // Read in the data from the PBP if (fread(buffer, readsize, 1, infile) < 0) { printf("ERROR: Could not read in the section data.\n"); + free(buffer); return -1; } // Write the contents of the buffer to the output file if (fwrite(buffer, readsize, 1, outfile) < 0) { printf("ERROR: Could not write out the section data.\n"); + free(buffer); return -1; } - // Clean up the buffer - free(buffer); - // Repeat if we haven't finished writing the file } while (size); + + // Clean up the buffer + free(buffer); + // Close the output file if (fclose(outfile) < 0) { printf("ERROR: Could not close the output file.\n");