Skip to content

Commit

Permalink
Use calloc() instead of malloc()
Browse files Browse the repository at this point in the history
Suggested by @iceman1001
Mainly for 8b6a274
Replaced the malloc() in getSamplesFromBufEx()
Added memory allocation result check for getSamplesFromBufEx(),
lf_read_internal(), and lf_sniff()
  • Loading branch information
wh201906 committed Nov 17, 2023
1 parent 3ee13c9 commit e82fb8b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
36 changes: 20 additions & 16 deletions client/src/cmddata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmddata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion client/src/cmdlf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion client/src/cmdlfhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e82fb8b

Please sign in to comment.