From e6432f05795ba0eaf1e34bb47b2a7f87a762de29 Mon Sep 17 00:00:00 2001
From: marshmellow42 <marshmellowrf@gmail.com>
Date: Wed, 13 May 2015 11:14:17 -0400
Subject: [PATCH] @iceman1001 s coverity scan fixes

---
 client/cmddata.c    |  2 +-
 client/cmdhfmf.c    | 10 ++++++----
 client/cmdhfmfu.c   |  2 +-
 client/cmdscript.c  |  2 +-
 client/mifarehost.c | 10 ++--------
 client/scripting.c  |  1 +
 client/util.c       |  2 +-
 7 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/client/cmddata.c b/client/cmddata.c
index 3ea75613..b9069bc1 100644
--- a/client/cmddata.c
+++ b/client/cmddata.c
@@ -1892,7 +1892,7 @@ int getSamples(const char *Cmd, bool silent)
 		PrintAndLog("Unpacking...");
 		BitstreamOut bout = { got, bits_per_sample * n,  0};
 		int j =0;
-		for (j = 0; j * bits_per_sample < n * 8 && j < sizeof(GraphBuffer); j++) {
+		for (j = 0; j * bits_per_sample < n * 8 && j < n; j++) {
 			uint8_t sample = getByte(bits_per_sample, &bout);
 			GraphBuffer[j] = ((int) sample )- 128;
 		}
diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c
index b96c9c1a..aa3b66dc 100644
--- a/client/cmdhfmf.c
+++ b/client/cmdhfmf.c
@@ -547,7 +547,7 @@ int CmdHF14AMfNested(const char *Cmd)
 	uint8_t trgKeyType = 0;
 	uint8_t SectorsCnt = 0;
 	uint8_t key[6] = {0, 0, 0, 0, 0, 0};
-	uint8_t keyBlock[13*6];
+	uint8_t keyBlock[14*6];
 	uint64_t key64 = 0;
 	bool transferToEml = false;
 	
@@ -1202,7 +1202,7 @@ int CmdHF14AMfELoad(const char *Cmd)
 	
 	if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
 
-	fnameptr += len;
+	fnameptr += len-4;
 
 	sprintf(fnameptr, ".eml"); 
 	
@@ -1311,7 +1311,7 @@ int CmdHF14AMfESave(const char *Cmd)
 		for (j = 0; j < 7; j++, fnameptr += 2)
 			sprintf(fnameptr, "%02X", buf[j]); 
 	} else {
-		fnameptr += len;
+		fnameptr += len-4;
 	}
 
 	// add file extension
@@ -1575,7 +1575,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
 		if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
 
 		memcpy(filename, Cmd, len);
-		fnameptr += len;
+		fnameptr += len-4;
 
 		sprintf(fnameptr, ".eml"); 
 	
@@ -1592,6 +1592,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
 			memset(buf, 0, sizeof(buf));
 			
 			if (fgets(buf, sizeof(buf), f) == NULL) {
+				fclose(f);
 				PrintAndLog("File reading error.");
 				return 2;
 			}
@@ -1600,6 +1601,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
 				if(strlen(buf) && feof(f))
 					break;
 				PrintAndLog("File content error. Block data must include 32 HEX symbols");
+				fclose(f);
 				return 2;
 			}
 			for (i = 0; i < 32; i += 2)
diff --git a/client/cmdhfmfu.c b/client/cmdhfmfu.c
index e7b90b4c..11282857 100644
--- a/client/cmdhfmfu.c
+++ b/client/cmdhfmfu.c
@@ -1103,7 +1103,7 @@ int CmdHF14AMfUDump(const char *Cmd){
 		sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin",
 			data[0],data[1], data[2], data[4],data[5],data[6], data[7]);
 	} else {
-		sprintf(fnameptr + fileNlen," .bin");
+		sprintf(fnameptr + fileNlen,".bin");
 	}
 
 	if ((fout = fopen(filename,"wb")) == NULL) { 
diff --git a/client/cmdscript.c b/client/cmdscript.c
index 928a216d..730f4e96 100644
--- a/client/cmdscript.c
+++ b/client/cmdscript.c
@@ -83,7 +83,7 @@ int CmdList(const char *Cmd)
     {
         while ((ep = readdir (dp)) != NULL)
         {
-            if(ep->d_name != NULL && str_ends_with(ep->d_name, ".lua"))
+            if(str_ends_with(ep->d_name, ".lua"))
                 PrintAndLog("%-16s %s", ep->d_name, "A script file");
         }
         (void) closedir (dp);
diff --git a/client/mifarehost.c b/client/mifarehost.c
index 35499b83..237979c1 100644
--- a/client/mifarehost.c
+++ b/client/mifarehost.c
@@ -354,10 +354,7 @@ int loadTraceCard(uint8_t *tuid) {
 	FillFileNameByUID(traceFileName, tuid, ".eml", 7);
 
 	f = fopen(traceFileName, "r");
-	if (!f) {
-		fclose(f);
-		return 1;
-	}
+	if (!f) return 1;
 	
 	blockNum = 0;
 		
@@ -394,10 +391,7 @@ int saveTraceCard(void) {
 	if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;
 	
 	f = fopen(traceFileName, "w+");
-	if ( !f ) {
-		fclose(f);
-		return 1;
-	}
+	if ( !f ) return 1;
 	
 	for (int i = 0; i < 64; i++) {  // blocks
 		for (int j = 0; j < 16; j++)  // bytes
diff --git a/client/scripting.c b/client/scripting.c
index d7f51c23..a7cf27d7 100644
--- a/client/scripting.c
+++ b/client/scripting.c
@@ -298,6 +298,7 @@ int setLuaPath( lua_State* L, const char* path )
     lua_pushstring( L, buf ); // push the new one
     lua_setfield( L, -2, "path" ); // set the field "path" in table at -2 with value at top of stack
     lua_pop( L, 1 ); // get rid of package table from top of stack
+    free(buf);
     return 0; // all done!
 }
 
diff --git a/client/util.c b/client/util.c
index 0dd6905e..cea9f7d7 100644
--- a/client/util.c
+++ b/client/util.c
@@ -124,7 +124,7 @@ char * sprint_hex(const uint8_t * data, const size_t len) {
 
 char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) {
 	
-	int maxLen = ( len > 1024) ? 1024 : len;
+	int maxLen = ( len > 1020) ? 1020 : len;
 	static char buf[1024];
 	memset(buf, 0x00, 1024);
 	char *tmp = buf;
-- 
2.39.5