diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index f7d8142ea..25baca523 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -382,7 +382,7 @@ int CmdHFFelicaDumpLite(const char *Cmd) { } uint64_t tracelen = resp.arg[1]; - uint8_t *trace = malloc(tracelen); + uint8_t *trace = calloc(tracelen, sizeof(uint8_t)); if ( trace == NULL ) { PrintAndLogEx(WARNING, "Cannot allocate memory for trace"); return 1; diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c index 742db2bf3..8518bfeb2 100644 --- a/client/cmdhficlass.c +++ b/client/cmdhficlass.c @@ -632,7 +632,7 @@ int CmdHFiClassELoad(const char *Cmd) { return 1; } - uint8_t *dump = malloc(fsize); + uint8_t *dump = calloc(fsize, sizeof(uint8_t)); if (!dump) { PrintAndLogDevice(WARNING, "error, cannot allocate memory "); fclose(f); @@ -725,7 +725,12 @@ int CmdHFiClassDecrypt(const char *Cmd) { return 2; } - uint8_t *decrypted = malloc(fsize); + uint8_t *decrypted = calloc(fsize, sizeof(uint8_t)); + if ( !decrypted ) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + fclose(f); + return 1; + } size_t bytes_read = fread(decrypted, 1, fsize, f); fclose(f); @@ -1626,7 +1631,12 @@ int CmdHFiClassReadTagFile(const char *Cmd) { return 1; } - uint8_t *dump = malloc(fsize); + uint8_t *dump = calloc(fsize, sizeof(uint8_t)); + if ( !dump ) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + fclose(f); + return 1; + } size_t bytes_read = fread(dump, 1, fsize, f); fclose(f); @@ -1783,8 +1793,12 @@ static int loadKeys(char *filename) { return 1; } - uint8_t *dump = malloc(fsize); - + uint8_t *dump = calloc(fsize, sizeof(uint8_t)); + if ( !dump ) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + fclose(f); + return 1; + } size_t bytes_read = fread(dump, 1, fsize, f); fclose(f); if (bytes_read > ICLASS_KEYS_MAX * 8){ diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index db36cd692..d87d5e228 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -2988,7 +2988,7 @@ int CmdHf14AMfDecryptBytes(const char *Cmd){ PrintAndLogEx(NORMAL, "ar enc\t%08X", ar_enc); PrintAndLogEx(NORMAL, "at enc\t%08X", at_enc); - uint8_t *data = malloc(len); + uint8_t *data = calloc(len, sizeof(uint8_t)); param_gethex_ex(Cmd, 3, data, &len); len >>= 1; tryDecryptWord( nt, ar_enc, at_enc, data, len); diff --git a/client/cmdhfmfu.c b/client/cmdhfmfu.c index 34063fc71..677490ec4 100644 --- a/client/cmdhfmfu.c +++ b/client/cmdhfmfu.c @@ -2002,7 +2002,11 @@ int CmdHF14AMfURestore(const char *Cmd){ return 1; } - uint8_t *dump = malloc(fsize); + uint8_t *dump = calloc(fsize, sizeof(uint8_t)); + if ( !dump ) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + return 1; + } // read all data size_t bytes_read = fread(dump, 1, fsize, f); diff --git a/client/cmdlfhitag.c b/client/cmdlfhitag.c index cdd74a8bd..c969bc4da 100644 --- a/client/cmdlfhitag.c +++ b/client/cmdlfhitag.c @@ -30,7 +30,7 @@ size_t nbytes(size_t nbits) { } int CmdLFHitagList(const char *Cmd) { - uint8_t *got = malloc(USB_CMD_DATA_SIZE); + uint8_t *got = calloc(USB_CMD_DATA_SIZE, sizeof(uint8_t)); if ( !got ) { PrintAndLogEx(WARNING, "Cannot allocate memory for trace"); return 2; diff --git a/client/cmdtrace.c b/client/cmdtrace.c index 68ca6266d..bb09a9d5d 100644 --- a/client/cmdtrace.c +++ b/client/cmdtrace.c @@ -481,7 +481,7 @@ int CmdTraceList(const char *Cmd) { // reserv some space. if (!trace) { printf("trace pointer not allocated\n"); - trace = malloc(USB_CMD_DATA_SIZE); + trace = calloc(USB_CMD_DATA_SIZE, sizeof(uint8_t)); } if ( isOnline ) { @@ -571,8 +571,8 @@ int CmdTraceLoad(const char *Cmd) { if ( trace ) free(trace); - trace = malloc(fsize); - if (trace == NULL) { + trace = calloc(fsize, sizeof(uint8_t)); + if (!trace) { PrintAndLogEx(FAILED, "Cannot allocate memory for trace"); fclose(f); return 2; diff --git a/client/loclass/cipherutils.c b/client/loclass/cipherutils.c index ba922f1e5..a6f7e92d1 100644 --- a/client/loclass/cipherutils.c +++ b/client/loclass/cipherutils.c @@ -155,17 +155,12 @@ void reverse_arraycopy(uint8_t* arr, uint8_t* dest, size_t len) } } -void printarr(char * name, uint8_t* arr, int len) -{ - int cx; - size_t outsize = 40+strlen(name)+len*5; - char* output = malloc(outsize); - memset(output, 0,outsize); - - int i ; +void printarr(char * name, uint8_t* arr, int len) { + int cx, i; + size_t outsize = 40 + strlen(name) + len*5; + char* output = calloc(outsize, sizeof(char)); cx = snprintf(output,outsize, "uint8_t %s[] = {", name); - for(i =0 ; i< len ; i++) - { + for (i=0; i < len; i++) { cx += snprintf(output+cx,outsize-cx,"0x%02x,",*(arr+i));//5 bytes per byte } cx += snprintf(output+cx,outsize-cx,"};"); @@ -173,17 +168,12 @@ void printarr(char * name, uint8_t* arr, int len) free(output); } -void printvar(char * name, uint8_t* arr, int len) -{ - int cx; - size_t outsize = 40+strlen(name)+len*2; - char* output = malloc(outsize); - memset(output, 0,outsize); - - int i ; +void printvar(char * name, uint8_t* arr, int len) { + int cx, i; + size_t outsize = 40 + strlen(name) + len*2; + char* output = calloc(outsize, sizeof(char)); cx = snprintf(output,outsize,"%s = ", name); - for(i =0 ; i< len ; i++) - { + for (i=0; i < len; i++) { cx += snprintf(output+cx,outsize-cx,"%02x",*(arr+i));//2 bytes per byte } @@ -191,19 +181,13 @@ void printvar(char * name, uint8_t* arr, int len) free(output); } -void printarr_human_readable(char * title, uint8_t* arr, int len) -{ - int cx; - size_t outsize = 100+strlen(title)+len*4; - char* output = malloc(outsize); - memset(output, 0,outsize); - - - int i; +void printarr_human_readable(char * title, uint8_t* arr, int len) { + int cx, i; + size_t outsize = 100 + strlen(title) + len*4; + char* output = calloc(outsize, sizeof(char)); cx = snprintf(output,outsize, "\n\t%s\n", title); - for(i =0 ; i< len ; i++) - { - if(i % 16 == 0) + for (i=0; i < len; i++) { + if (i % 16 == 0) cx += snprintf(output+cx,outsize-cx,"\n%02x| ", i ); cx += snprintf(output+cx,outsize-cx, "%02x ",*(arr+i)); } diff --git a/client/loclass/elite_crack.c b/client/loclass/elite_crack.c index 403696c89..e24a9765e 100644 --- a/client/loclass/elite_crack.c +++ b/client/loclass/elite_crack.c @@ -542,7 +542,11 @@ int bruteforceFile(const char *filename, uint16_t keytable[]) { return 1; } - uint8_t *dump = malloc(fsize); + uint8_t *dump = calloc(fsize, sizeof(uint8_t)); + if ( !dump ) { + PrintAndLogDevice(WARNING, "Failed to allocate memory"); + return 2; + } size_t bytes_read = fread(dump, 1, fsize, f); if (f) fclose(f); diff --git a/client/scripting.c b/client/scripting.c index 74e005ba8..2ea8be330 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -71,8 +71,8 @@ static int l_GetFromBigBuf(lua_State *L){ startindex = luaL_checknumber(L, 2); } - uint8_t *data = malloc(len); - if ( data == NULL ) { + uint8_t *data = calloc(len, sizeof(uint8_t)); + if ( !data ) { //signal error by returning Nil, errorstring lua_pushnil(L); lua_pushstring(L,"Allocating memory failed"); @@ -88,8 +88,7 @@ static int l_GetFromBigBuf(lua_State *L){ //Push it as a string lua_pushlstring(L,(const char *)data, len); - if (data) - free(data); + free(data); return 1;// return 1 to signal one return value } /** @@ -641,7 +640,7 @@ int setLuaPath( lua_State* L, const char* path ) { lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1) const char* cur_path = lua_tostring( L, -1 ); // grab path string from top of stack int requiredLength = strlen(cur_path)+ strlen(path)+10; //A few bytes too many, whatever we can afford it - char * buf = malloc(requiredLength); + char * buf = calloc(requiredLength, sizeof(char)); snprintf(buf, requiredLength, "%s;%s", cur_path, path); lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5 lua_pushstring( L, buf ); // push the new one