From 609d71437f31408be46dfe96c9bc354f77afa3f5 Mon Sep 17 00:00:00 2001 From: Robert MacGregor Date: Sat, 12 Mar 2016 17:20:41 -0500 Subject: [PATCH 1/4] Remove close/reopen logic for file handle and fix Ecc crash on empty --- Sources/Ecc/Main.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Sources/Ecc/Main.cpp b/Sources/Ecc/Main.cpp index a17591046..b47863121 100644 --- a/Sources/Ecc/Main.cpp +++ b/Sources/Ecc/Main.cpp @@ -239,7 +239,7 @@ enum ESStatus }; /* Determine whether or not our target ES file is indeed valid input. */ -ESStatus GetESStatus(char *filename) +ESStatus GetESStatus() { ESStatus result = ESStatus::Empty; @@ -249,7 +249,7 @@ ESStatus GetESStatus(char *filename) char* temporaryBuffer = (char*)malloc(length); fseek(_fInput, 0, SEEK_SET); fread(temporaryBuffer, length, 1, _fInput); - fclose(_fInput); + fseek(_fInput, 0, SEEK_SET); // Loop through each line char* currentSequence = strtok(temporaryBuffer, "\n"); @@ -315,18 +315,11 @@ ESStatus GetESStatus(char *filename) result = ESStatus::Good; free(temporaryBuffer); - if (result == ESStatus::Good) - _fInput = FOpen(filename, "r"); - return result; } } while(currentSequence = strtok(NULL, "\n")); - free(temporaryBuffer); - if (result == ESStatus::Good) - _fInput = FOpen(filename, "r"); - return result; } @@ -388,7 +381,7 @@ int main(int argc, char *argv[]) _fInput = FOpen(argv[1], "r"); // Make sure we're loading a valid ES file - ESStatus status = GetESStatus(argv[1]); + ESStatus status = GetESStatus(); switch (status) { From d69cb55c21394a66cae01013ed48cae98427d786 Mon Sep 17 00:00:00 2001 From: Robert MacGregor Date: Sat, 12 Mar 2016 17:27:34 -0500 Subject: [PATCH 2/4] Add explicit 0-length check out of paranoia --- Sources/Ecc/Main.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/Ecc/Main.cpp b/Sources/Ecc/Main.cpp index b47863121..71178aaf6 100644 --- a/Sources/Ecc/Main.cpp +++ b/Sources/Ecc/Main.cpp @@ -246,6 +246,11 @@ ESStatus GetESStatus() // Read a temporary buffer of the entire file contents fseek(_fInput, 0, SEEK_END); size_t length = ftell(_fInput); + + // Hard-stop on Empty out of paranoia + if (length == 0) + return result; + char* temporaryBuffer = (char*)malloc(length); fseek(_fInput, 0, SEEK_SET); fread(temporaryBuffer, length, 1, _fInput); From 9766df5f5b3c68e15769d624cfb8328eca9c303d Mon Sep 17 00:00:00 2001 From: Robert MacGregor Date: Sat, 12 Mar 2016 17:31:51 -0500 Subject: [PATCH 3/4] Forgot to close dangling file handles --- Sources/Ecc/Main.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/Ecc/Main.cpp b/Sources/Ecc/Main.cpp index 71178aaf6..8168f6e85 100644 --- a/Sources/Ecc/Main.cpp +++ b/Sources/Ecc/Main.cpp @@ -391,9 +391,15 @@ int main(int argc, char *argv[]) switch (status) { case ESStatus::Empty: + { + fclose(_fInput); return EXIT_SUCCESS; + } case ESStatus::Error: + { + fclose(_fInput); return EXIT_FAILURE; + } } //printf("%s\n", argv[1]); From 1a50ba665a21676ec06b21bb6ba744b32ac2c4bf Mon Sep 17 00:00:00 2001 From: Robert MacGregor Date: Sat, 12 Mar 2016 17:36:50 -0500 Subject: [PATCH 4/4] Check for NULL return from malloc in es vertification code and print error if es verification fails --- Sources/Ecc/Main.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/Ecc/Main.cpp b/Sources/Ecc/Main.cpp index 8168f6e85..3f88e62d4 100644 --- a/Sources/Ecc/Main.cpp +++ b/Sources/Ecc/Main.cpp @@ -252,6 +252,10 @@ ESStatus GetESStatus() return result; char* temporaryBuffer = (char*)malloc(length); + + if (!temporaryBuffer) + return ESStatus::Error; + fseek(_fInput, 0, SEEK_SET); fread(temporaryBuffer, length, 1, _fInput); fseek(_fInput, 0, SEEK_SET); @@ -398,6 +402,7 @@ int main(int argc, char *argv[]) case ESStatus::Error: { fclose(_fInput); + printf("Ecc encountered an error during the es verification.\n"); return EXIT_FAILURE; } }