Apply suggestions from @henrygab

Check if memory allocation fails
Fix memory leak
Initialize struct in declaration
Add/Fix some notes
Remove unlikely() in favor of readability
Remove a hard-coded magic number
This commit is contained in:
wh201906 committed 2023-11-15 18:04:52 +08:00
1 parent 835b7a9150
commit 3ee13c9ba6
12 files changed
+113 -44

No files matched your search

+36
View File
@@ -1012,6 +1012,10 @@ static int CmdUndecimate(const char *Cmd) {
//We have memory, don't we?
int *swap = calloc(MAX_GRAPH_TRACE_LEN, sizeof(int));
if (swap == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
uint32_t g_index = 0, s_index = 0;
while (g_index < g_GraphTraceLen && s_index + factor < MAX_GRAPH_TRACE_LEN) {
int count = 0;
@@ -1709,6 +1713,10 @@ int CmdHpf(const char *Cmd) {
CLIParserFree(ctx);
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
removeSignalOffset(bits, size);
// push it back to graph
@@ -2106,6 +2114,10 @@ static int CmdLoad(const char *Cmd) {
if (nofix == false) {
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
removeSignalOffset(bits, size);
@@ -2244,6 +2256,10 @@ int CmdNorm(const char *Cmd) {
}
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
@@ -2391,6 +2407,10 @@ static int CmdDirectionalThreshold(const char *Cmd) {
// set signal properties low/high/mean/amplitude and isnoice detection
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);
@@ -2435,6 +2455,10 @@ static int CmdZerocrossings(const char *Cmd) {
}
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
@@ -2749,6 +2773,10 @@ static int CmdDataIIR(const char *Cmd) {
iceSimple_Filter(g_GraphBuffer, g_GraphTraceLen, k);
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
@@ -3377,6 +3405,10 @@ static int CmdCenterThreshold(const char *Cmd) {
// set signal properties low/high/mean/amplitude and isnoice detection
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);
@@ -3423,6 +3455,10 @@ static int CmdEnvelope(const char *Cmd) {
envelope_square(g_GraphBuffer, g_GraphBuffer, g_GraphTraceLen);
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);
+12 -6
View File
@@ -432,6 +432,10 @@ int CmdFlexdemod(const char *Cmd) {
int i, j, start, bit, sum;
int *data = malloc(g_GraphTraceLen * sizeof(int));
if (data == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
memcpy(data, g_GraphBuffer, g_GraphTraceLen);
size_t size = g_GraphTraceLen;
@@ -697,7 +701,7 @@ int CmdLFConfig(const char *Cmd) {
static int lf_read_internal(bool realtime, bool verbose, uint64_t samples) {
if (!g_session.pm3_present) return PM3_ENOTTY;
lf_sample_payload_t payload;
lf_sample_payload_t payload = {0};
payload.realtime = realtime;
payload.verbose = verbose;
@@ -720,7 +724,7 @@ static int lf_read_internal(bool realtime, bool verbose, uint64_t samples) {
SendCommandNG(CMD_LF_ACQ_RAW_ADC, (uint8_t *)&payload, sizeof(payload));
if (is_trigger_threshold_set) {
size_t first_receive_len = 32; // larger than the response of CMD_WTX
// wait until a bunch of data arrives
// Wait until a bunch of data arrives
first_receive_len = WaitForRawDataTimeout(realtimeBuf, first_receive_len, -1, false);
sample_bytes = WaitForRawDataTimeout(realtimeBuf + first_receive_len, sample_bytes - first_receive_len, 1000 + FPGA_LOAD_WAIT_TIME, true);
sample_bytes += first_receive_len;
@@ -759,7 +763,8 @@ int lf_read(bool verbose, uint64_t samples) {
}
int CmdLFRead(const char *Cmd) {
// In real-time mode, the first few bytes might be the response of CMD_WTX rather than the real samples
// In real-time mode, the first few bytes might be the response of CMD_WTX
// rather than the real samples if the LF FPGA image is not ready.
CLIParserContext *ctx;
CLIParserInit(&ctx, "lf read",
"Sniff low frequency signal.\n"
@@ -805,7 +810,7 @@ int CmdLFRead(const char *Cmd) {
int lf_sniff(bool realtime, bool verbose, uint64_t samples) {
if (!g_session.pm3_present) return PM3_ENOTTY;
lf_sample_payload_t payload;
lf_sample_payload_t payload = {0};
payload.realtime = realtime;
payload.verbose = verbose;
@@ -828,7 +833,7 @@ int lf_sniff(bool realtime, bool verbose, uint64_t samples) {
SendCommandNG(CMD_LF_SNIFF_RAW_ADC, (uint8_t *)&payload, sizeof(payload));
if (is_trigger_threshold_set) {
size_t first_receive_len = 32; // larger than the response of CMD_WTX
// wait until a bunch of data arrives
// Wait until a bunch of data arrives
first_receive_len = WaitForRawDataTimeout(realtimeBuf, first_receive_len, -1, false);
sample_bytes = WaitForRawDataTimeout(realtimeBuf + first_receive_len, sample_bytes - first_receive_len, 1000 + FPGA_LOAD_WAIT_TIME, true);
sample_bytes += first_receive_len;
@@ -863,7 +868,8 @@ int lf_sniff(bool realtime, bool verbose, uint64_t samples) {
}
int CmdLFSniff(const char *Cmd) {
// In real-time mode, the first few bytes might be the response of CMD_WTX rather than the real samples
// In real-time mode, the first few bytes might be the response of CMD_WTX
// rather than the real samples if the LF FPGA image is not ready.
CLIParserContext *ctx;
CLIParserInit(&ctx, "lf sniff",
"Sniff low frequency signal. You need to configure the LF part on the Proxmark3 device manually.\n"
+5
View File
@@ -118,6 +118,10 @@ int demodHID(bool verbose) {
uint32_t hi2 = 0, hi = 0, lo = 0;
uint8_t *bits = malloc(g_GraphTraceLen);
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - " _RED_("HID not enough samples"));
@@ -142,6 +146,7 @@ int demodHID(bool verbose) {
else
PrintAndLogEx(DEBUG, "DEBUG: Error - " _RED_("HID error demoding fsk %d"), idx);
free(bits);
return PM3_ESOFT;
}
+4
View File
@@ -404,6 +404,10 @@ static int CmdIndalaDemodAlt(const char *Cmd) {
// worst case with g_GraphTraceLen=40000 is < 4096
// under normal conditions it's < 2048
uint8_t *data = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
if (data == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t datasize = getFromGraphBuf(data);
uint8_t rawbits[4096] = {0};
+4
View File
@@ -67,6 +67,10 @@ int demodIOProx(bool verbose) {
(void) verbose; // unused so far
int idx = 0, retval = PM3_SUCCESS;
uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
if (size < 65) {
PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox not enough samples in GraphBuffer");
+4
View File
@@ -104,6 +104,10 @@ int demodParadox(bool verbose, bool oldChksum) {
(void) verbose; // unused so far
//raw fsk demod no manchester decoding no start bit finding just get binary from wave
uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - Paradox not enough samples");
+4
View File
@@ -44,6 +44,10 @@ int demodPyramid(bool verbose) {
(void) verbose; // unused so far
//raw fsk demod no manchester decoding no start bit finding just get binary from wave
uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
if (bits == NULL) {
PrintAndLogEx(FAILED, "failed to allocate memory");
return PM3_EMALLOC;
}
size_t size = getFromGraphBuf(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - Pyramid not enough samples");
+12 -12
View File
@@ -350,7 +350,7 @@ __attribute__((force_align_arg_pointer))
bool commfailed = false;
PacketResponseNG rx;
PacketResponseNGRaw rx_raw;
// stash the last state of is_receiving_raw, to detect if state changed
// Stash the last state of is_receiving_raw, to detect if state changed
bool is_receiving_raw_last = false;
#if defined(__MACH__) && defined(__APPLE__)
@@ -398,7 +398,7 @@ __attribute__((force_align_arg_pointer))
}
}
} else {
// ignore data when bufferPos >= bufferLen and is_receiving_raw has not been set to false
// Ignore data when bufferPos >= bufferLen and is_receiving_raw has not been set to false
uint8_t dummyData[64];
uint32_t dummyLen;
uart_receive(sp, dummyData, sizeof(dummyData), &dummyLen);
@@ -407,7 +407,7 @@ __attribute__((force_align_arg_pointer))
if (is_receiving_raw_last) {
// is_receiving_raw changed from true to false
// set the buffer as undefined
// Set the buffer as undefined
// comm_raw_data == NULL is used in SetCommunicationReceiveMode()
__atomic_store_n(&comm_raw_data, NULL, __ATOMIC_SEQ_CST);
}
@@ -849,11 +849,11 @@ size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, boo
while (pos < len) {
if (kbd_enter_pressed()) {
// send anything to stop the transfer
// Send anything to stop the transfer
PrintAndLogEx(INFO, "Stopping");
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
// for ms_timeout == -1, pos < len might always be true
// For ms_timeout == -1, pos < len might always be true
// so user need a spectial way to break this loop
if (ms_timeout == (size_t) - 1) {
break;
@@ -862,15 +862,15 @@ size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, boo
pos = __atomic_load_n(&comm_raw_pos, __ATOMIC_SEQ_CST);
// check the timeout if pos is not updated
// Check the timeout if pos is not updated
if (last_pos == pos) {
uint64_t tmp_clk = __atomic_load_n(&timeout_start_time, __ATOMIC_SEQ_CST);
// if ms_timeout == -1, the loop can only be breaked by pressing Enter or receiving enough data
// If ms_timeout == -1, the loop can only be breaked by pressing Enter or receiving enough data
if ((ms_timeout != (size_t) - 1) && (msclock() - tmp_clk > ms_timeout)) {
break;
}
} else {
// print when (print_counter % 64) == 0
// Print process when (print_counter % 64) == 0
if (show_process && (print_counter & 0x3F) == 0) {
PrintAndLogEx(INFO, "[%zu/%zu]", pos, len);
}
@@ -881,11 +881,11 @@ size_t WaitForRawDataTimeout(uint8_t *buffer, size_t len, size_t ms_timeout, boo
msleep(10);
}
if (pos == len && (ms_timeout != (size_t) - 1)) {
// if ms_timeout != -1, when the desired data is received, tell the arm side
// If ms_timeout != -1, when the desired data is received, tell the arm side
// to stop the current process, and wait for some time to make sure the process
// has been stopped
// if ms_timeout == -1, the user might not want to break the existing process
// on the arm side
// has been stopped.
// If ms_timeout == -1, the user might not want to break the existing process
// on the arm side.
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
msleep(ms_timeout);
}