Use calloc() instead of malloc()

Suggested by @iceman1001
Mainly for 8b6a274e28
Replaced the malloc() in getSamplesFromBufEx()
Added memory allocation result check for getSamplesFromBufEx(),
lf_read_internal(), and lf_sniff()
This commit is contained in:
wh201906 2023-11-17 12:23:58 +08:00
parent 3ee13c9ba6
commit e82fb8b67f
No known key found for this signature in database
4 changed files with 31 additions and 19 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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;