From e82fb8b67fe83a6c5ee9dfa8d8de57eb85a98895 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Fri, 17 Nov 2023 12:23:58 +0800 Subject: [PATCH] Use calloc() instead of malloc() Suggested by @iceman1001 Mainly for 8b6a274e289cd96f16616de741f0d02fb10d1e6e Replaced the malloc() in getSamplesFromBufEx() Added memory allocation result check for getSamplesFromBufEx(), lf_read_internal(), and lf_sniff() --- client/src/cmddata.c | 36 ++++++++++++++++++++---------------- client/src/cmddata.h | 2 +- client/src/cmdlf.c | 10 +++++++++- client/src/cmdlfhid.c | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/client/src/cmddata.c b/client/src/cmddata.c index 734b87644..7445f3759 100644 --- a/client/src/cmddata.c +++ b/client/src/cmddata.c @@ -1712,7 +1712,7 @@ int CmdHpf(const char *Cmd) { CLIExecWithReturn(ctx, Cmd, argtable, true); CLIParserFree(ctx); - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -1786,12 +1786,10 @@ int getSamplesEx(uint32_t start, uint32_t end, bool verbose, bool ignore_lf_conf bits_per_sample = sc->bits_per_sample; } - getSamplesFromBufEx(got, n, bits_per_sample, verbose); - - return PM3_SUCCESS; + return getSamplesFromBufEx(got, n, bits_per_sample, verbose);; } -void getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_sample, bool verbose) { +int getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_sample, bool verbose) { size_t max_num = MIN(sample_num, MAX_GRAPH_TRACE_LEN); @@ -1816,7 +1814,11 @@ void getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_samp g_GraphTraceLen = max_num; } - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); + 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); @@ -1825,6 +1827,8 @@ void getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_samp setClockGrid(0, 0); g_DemodBufferLen = 0; RepaintGraphWindow(); + + return PM3_SUCCESS; } static int CmdSamples(const char *Cmd) { @@ -2113,11 +2117,11 @@ static int CmdLoad(const char *Cmd) { PrintAndLogEx(SUCCESS, "loaded " _YELLOW_("%zu") " samples", g_GraphTraceLen); if (nofix == false) { - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { - PrintAndLogEx(FAILED, "failed to allocate memory"); - return PM3_EMALLOC; - } + PrintAndLogEx(FAILED, "failed to allocate memory"); + return PM3_EMALLOC; + } size_t size = getFromGraphBuf(bits); removeSignalOffset(bits, size); @@ -2255,7 +2259,7 @@ int CmdNorm(const char *Cmd) { } } - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -2406,7 +2410,7 @@ static int CmdDirectionalThreshold(const char *Cmd) { directionalThreshold(g_GraphBuffer, g_GraphBuffer, g_GraphTraceLen, up, down); // set signal properties low/high/mean/amplitude and isnoice detection - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -2454,7 +2458,7 @@ static int CmdZerocrossings(const char *Cmd) { } } - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -2772,7 +2776,7 @@ static int CmdDataIIR(const char *Cmd) { iceSimple_Filter(g_GraphBuffer, g_GraphTraceLen, k); - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -3404,7 +3408,7 @@ static int CmdCenterThreshold(const char *Cmd) { centerThreshold(g_GraphBuffer, g_GraphBuffer, g_GraphTraceLen, up, down); // set signal properties low/high/mean/amplitude and isnoice detection - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -3454,7 +3458,7 @@ static int CmdEnvelope(const char *Cmd) { envelope_square(g_GraphBuffer, g_GraphBuffer, g_GraphTraceLen); - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; diff --git a/client/src/cmddata.h b/client/src/cmddata.h index c9f7398be..0767412cb 100644 --- a/client/src/cmddata.h +++ b/client/src/cmddata.h @@ -86,7 +86,7 @@ int AutoCorrelate(const int *in, int *out, size_t len, size_t window, bool SaveG int getSamples(uint32_t n, bool verbose); int getSamplesEx(uint32_t start, uint32_t end, bool verbose, bool ignore_lf_config); -void getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_sample, bool verbose); +int getSamplesFromBufEx(uint8_t *data, size_t sample_num, uint8_t bits_per_sample, bool verbose); void setClockGrid(uint32_t clk, int offset); int directionalThreshold(const int *in, int *out, size_t len, int8_t up, int8_t down); diff --git a/client/src/cmdlf.c b/client/src/cmdlf.c index 3c4e44af8..2247236fe 100644 --- a/client/src/cmdlf.c +++ b/client/src/cmdlf.c @@ -431,7 +431,7 @@ int CmdFlexdemod(const char *Cmd) { #endif int i, j, start, bit, sum; - int *data = malloc(g_GraphTraceLen * sizeof(int)); + int *data = calloc(g_GraphTraceLen, sizeof(int)); if (data == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC; @@ -717,6 +717,10 @@ static int lf_read_internal(bool realtime, bool verbose, uint64_t samples) { if (realtime) { uint8_t *realtimeBuf = calloc(samples, sizeof(uint8_t)); + if (realtimeBuf == NULL) { + PrintAndLogEx(FAILED, "failed to allocate memory"); + return PM3_EMALLOC; + } size_t sample_bytes = samples * bits_per_sample; sample_bytes = (sample_bytes / 8) + (sample_bytes % 8 != 0); @@ -826,6 +830,10 @@ int lf_sniff(bool realtime, bool verbose, uint64_t samples) { if (realtime) { uint8_t *realtimeBuf = calloc(samples, sizeof(uint8_t)); + if (realtimeBuf == NULL) { + PrintAndLogEx(FAILED, "failed to allocate memory"); + return PM3_EMALLOC; + } size_t sample_bytes = samples * bits_per_sample; sample_bytes = (sample_bytes / 8) + (sample_bytes % 8 != 0); diff --git a/client/src/cmdlfhid.c b/client/src/cmdlfhid.c index e8b0d179e..0526b16db 100644 --- a/client/src/cmdlfhid.c +++ b/client/src/cmdlfhid.c @@ -117,7 +117,7 @@ int demodHID(bool verbose) { //raw fsk demod no manchester decoding no start bit finding just get binary from wave uint32_t hi2 = 0, hi = 0, lo = 0; - uint8_t *bits = malloc(g_GraphTraceLen); + uint8_t *bits = calloc(g_GraphTraceLen, sizeof(uint8_t)); if (bits == NULL) { PrintAndLogEx(FAILED, "failed to allocate memory"); return PM3_EMALLOC;