Fix some missing frees, null dereferences and missing initialisation picked up by clang-analyzer

This commit is contained in:
nvx 2024-01-16 00:22:53 +10:00
parent 5db2276602
commit d2d6f80d11
11 changed files with 24 additions and 15 deletions

View file

@ -493,6 +493,8 @@ float brute_force_benchmark(void) {
if (!read_bench_data(test_candidates)) { if (!read_bench_data(test_candidates)) {
PrintAndLogEx(NORMAL, "Couldn't read benchmark data. Assuming brute force rate of %1.0f states per second", DEFAULT_BRUTE_FORCE_RATE); PrintAndLogEx(NORMAL, "Couldn't read benchmark data. Assuming brute force rate of %1.0f states per second", DEFAULT_BRUTE_FORCE_RATE);
free(test_candidates[0].states[ODD_STATE]);
free(test_candidates[0].states[EVEN_STATE]);
return DEFAULT_BRUTE_FORCE_RATE; return DEFAULT_BRUTE_FORCE_RATE;
} }
@ -515,5 +517,3 @@ float brute_force_benchmark(void) {
test_candidates[0].len[EVEN_STATE] = 0; test_candidates[0].len[EVEN_STATE] = 0;
return bf_rate; return bf_rate;
} }

View file

@ -832,7 +832,7 @@ int CmdHF14ASim(const char *Cmd) {
clearCommandBuffer(); clearCommandBuffer();
SendCommandNG(CMD_HF_ISO14443A_SIMULATE, (uint8_t *)&payload, sizeof(payload)); SendCommandNG(CMD_HF_ISO14443A_SIMULATE, (uint8_t *)&payload, sizeof(payload));
PacketResponseNG resp; PacketResponseNG resp = {0};
sector_t *k_sector = NULL; sector_t *k_sector = NULL;
size_t k_sectors_cnt = MIFARE_4K_MAXSECTOR; size_t k_sectors_cnt = MIFARE_4K_MAXSECTOR;

View file

@ -968,6 +968,7 @@ static int CmdHF15Info(const char *Cmd) {
if (scan) { if (scan) {
if (getUID(true, false, uid) != PM3_SUCCESS) { if (getUID(true, false, uid) != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "no tag found"); PrintAndLogEx(WARNING, "no tag found");
free(packet);
return PM3_EINVARG; return PM3_EINVARG;
} }
uidlen = HF15_UID_LENGTH; uidlen = HF15_UID_LENGTH;
@ -1625,6 +1626,7 @@ static int CmdHF15WriteDsfid(const char *Cmd) {
if (scan) { if (scan) {
if (getUID(verbose, false, uid) != PM3_SUCCESS) { if (getUID(verbose, false, uid) != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "no tag found"); PrintAndLogEx(WARNING, "no tag found");
free(packet);
return PM3_EINVARG; return PM3_EINVARG;
} }
uidlen = HF15_UID_LENGTH; uidlen = HF15_UID_LENGTH;
@ -1743,6 +1745,7 @@ static int CmdHF15Dump(const char *Cmd) {
// default fallback to scan for tag. Overriding unaddress parameter // default fallback to scan for tag. Overriding unaddress parameter
if (scan) { if (scan) {
if (getUID(verbose, false, uid) != PM3_SUCCESS) { if (getUID(verbose, false, uid) != PM3_SUCCESS) {
free(packet);
PrintAndLogEx(WARNING, "no tag found"); PrintAndLogEx(WARNING, "no tag found");
return PM3_EINVARG; return PM3_EINVARG;
} }
@ -2098,6 +2101,7 @@ static int CmdHF15Readmulti(const char *Cmd) {
if (unaddressed == false) { if (unaddressed == false) {
if (scan) { if (scan) {
if (getUID(verbose, false, uid) != PM3_SUCCESS) { if (getUID(verbose, false, uid) != PM3_SUCCESS) {
free(packet);
PrintAndLogEx(WARNING, "no tag found"); PrintAndLogEx(WARNING, "no tag found");
return PM3_EINVARG; return PM3_EINVARG;
} }
@ -2250,6 +2254,7 @@ static int CmdHF15Readblock(const char *Cmd) {
// default fallback to scan for tag. Overriding unaddress parameter // default fallback to scan for tag. Overriding unaddress parameter
if (scan) { if (scan) {
if (getUID(verbose, false, uid) != PM3_SUCCESS) { if (getUID(verbose, false, uid) != PM3_SUCCESS) {
free(packet);
PrintAndLogEx(WARNING, "no tag found"); PrintAndLogEx(WARNING, "no tag found");
return PM3_EINVARG; return PM3_EINVARG;
} }

View file

@ -281,12 +281,12 @@ static int CmdHFCryptoRFDump(const char *Cmd) {
bool m512 = arg_get_lit(ctx, 3); bool m512 = arg_get_lit(ctx, 3);
CLIParserFree(ctx); CLIParserFree(ctx);
if (m512 + m64 > 1) { if (m512 + m64 != 1) {
PrintAndLogEx(INFO, "Select only one card memory size"); PrintAndLogEx(INFO, "Select only one card memory size");
return PM3_EINVARG; return PM3_EINVARG;
} }
uint16_t cardsize = 0; uint16_t cardsize;
uint8_t blocks = 0; uint8_t blocks = 0;
if (m64) { if (m64) {
cardsize = (512 / 8) + 4; cardsize = (512 / 8) + 4;
@ -550,4 +550,3 @@ int CmdHFCryptoRF(const char *Cmd) {
clearCommandBuffer(); clearCommandBuffer();
return CmdsParse(CommandTable, Cmd); return CmdsParse(CommandTable, Cmd);
} }

View file

@ -2043,6 +2043,7 @@ int infoHF_EMRTD_offline(const char *path) {
// coverity scan CID 395630, // coverity scan CID 395630,
if (data == NULL) { if (data == NULL) {
free(filepath);
return PM3_ESOFT; return PM3_ESOFT;
} }

View file

@ -292,6 +292,7 @@ int CmdLFCommandRead(const char *Cmd) {
payload.samples = samples; payload.samples = samples;
payload.keep_field_on = keep_field_on; payload.keep_field_on = keep_field_on;
payload.verbose = verbose; payload.verbose = verbose;
memset(payload.symbol_extra, 0, sizeof(payload.symbol_extra));
if (add_crc_ht && (cmd_len <= 120)) { if (add_crc_ht && (cmd_len <= 120)) {
// Hitag 1, Hitag S, ZX8211 // Hitag 1, Hitag S, ZX8211
@ -734,6 +735,7 @@ static int lf_read_internal(bool realtime, bool verbose, uint64_t samples) {
int result = set_fpga_mode(FPGA_BITSTREAM_LF); int result = set_fpga_mode(FPGA_BITSTREAM_LF);
if (result != PM3_SUCCESS) { if (result != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "failed to load LF bitstream to FPGA"); PrintAndLogEx(FAILED, "failed to load LF bitstream to FPGA");
free(realtimeBuf);
return result; return result;
} }
@ -860,6 +862,7 @@ int lf_sniff(bool realtime, bool verbose, uint64_t samples) {
int result = set_fpga_mode(FPGA_BITSTREAM_LF); int result = set_fpga_mode(FPGA_BITSTREAM_LF);
if (result != PM3_SUCCESS) { if (result != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "failed to load LF bitstream to FPGA"); PrintAndLogEx(FAILED, "failed to load LF bitstream to FPGA");
free(realtimeBuf);
return result; return result;
} }

View file

@ -1853,7 +1853,7 @@ static int CmdT55xxReadTrace(const char *Cmd) {
ct = localtime_r(&now, &tm_buf); ct = localtime_r(&now, &tm_buf);
#endif #endif
if (data.year > ct->tm_year - 110) if (ct != NULL && (data.year > ct->tm_year - 110))
data.year += 2000; data.year += 2000;
else else
data.year += 2010; data.year += 2010;
@ -4419,4 +4419,3 @@ int CmdLFT55XX(const char *Cmd) {
clearCommandBuffer(); clearCommandBuffer();
return CmdsParse(CommandTable, Cmd); return CmdsParse(CommandTable, Cmd);
} }

View file

@ -1091,6 +1091,7 @@ static int CmdSmartBruteforceSFI(const char *Cmd) {
if (json_is_object(data) == false) { if (json_is_object(data) == false) {
PrintAndLogEx(ERR, "\ndata %d is not an object\n", i + 1); PrintAndLogEx(ERR, "\ndata %d is not an object\n", i + 1);
json_decref(root); json_decref(root);
free(buf);
return PM3_ESOFT; return PM3_ESOFT;
} }
@ -1098,6 +1099,7 @@ static int CmdSmartBruteforceSFI(const char *Cmd) {
if (json_is_string(jaid) == false) { if (json_is_string(jaid) == false) {
PrintAndLogEx(ERR, "\nAID data [%d] is not a string", i + 1); PrintAndLogEx(ERR, "\nAID data [%d] is not a string", i + 1);
json_decref(root); json_decref(root);
free(buf);
return PM3_ESOFT; return PM3_ESOFT;
} }
@ -1459,5 +1461,3 @@ bool smart_select(bool verbose, smart_card_atr_t *atr) {
return true; return true;
} }

View file

@ -1333,11 +1333,12 @@ static int l_cwd(lua_State *L) {
while (GetCurrentDir(cwd, path_len) == NULL) { while (GetCurrentDir(cwd, path_len) == NULL) {
if (errno == ERANGE) { // Need bigger buffer if (errno == ERANGE) { // Need bigger buffer
path_len += 10; // if buffer was too small add 10 characters and try again path_len += 10; // if buffer was too small add 10 characters and try again
cwd = realloc(cwd, path_len); char* cwdNew = realloc(cwd, path_len);
if (cwd == NULL) { if (cwdNew == NULL) {
free(cwd); free(cwd);
return returnToLuaWithError(L, "Failed to allocate memory"); return returnToLuaWithError(L, "Failed to allocate memory");
} }
cwd = cwdNew;
} else { } else {
free(cwd); free(cwd);
return returnToLuaWithError(L, "Failed to get current working directory"); return returnToLuaWithError(L, "Failed to get current working directory");

View file

@ -85,13 +85,14 @@ static int uart_reconfigure_timeouts_polling(serial_port sp) {
serial_port uart_open(const char *pcPortName, uint32_t speed, bool slient) { serial_port uart_open(const char *pcPortName, uint32_t speed, bool slient) {
char acPortName[255] = {0}; char acPortName[255] = {0};
serial_port_windows_t *sp = calloc(sizeof(serial_port_windows_t), sizeof(uint8_t)); serial_port_windows_t *sp = calloc(sizeof(serial_port_windows_t), sizeof(uint8_t));
sp->hSocket = INVALID_SOCKET; // default: serial port
if (sp == 0) { if (sp == 0) {
PrintAndLogEx(WARNING, "UART failed to allocate memory\n"); PrintAndLogEx(WARNING, "UART failed to allocate memory\n");
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
sp->hSocket = INVALID_SOCKET; // default: serial port
sp->udpBuffer = NULL; sp->udpBuffer = NULL;
rx_empty_counter = 0; rx_empty_counter = 0;
g_conn.send_via_local_ip = false; g_conn.send_via_local_ip = false;

View file

@ -119,7 +119,7 @@ int searchHomeFilePath(char **foundpath, const char *subdir, const char *filenam
pathlen += strlen(subdir); pathlen += strlen(subdir);
char *tmp = realloc(path, pathlen * sizeof(char)); char *tmp = realloc(path, pathlen * sizeof(char));
if (tmp == NULL) { if (tmp == NULL) {
//free(path); free(path);
return PM3_EMALLOC; return PM3_EMALLOC;
} }
path = tmp; path = tmp;
@ -156,7 +156,7 @@ int searchHomeFilePath(char **foundpath, const char *subdir, const char *filenam
pathlen += strlen(filename); pathlen += strlen(filename);
char *tmp = realloc(path, pathlen * sizeof(char)); char *tmp = realloc(path, pathlen * sizeof(char));
if (tmp == NULL) { if (tmp == NULL) {
//free(path); free(path);
return PM3_EMALLOC; return PM3_EMALLOC;
} }