cppcheck: a few mem alloc checks

This commit is contained in:
Philippe Teuwen 2025-03-24 10:44:01 +01:00
commit 678b387c3f
8 changed files with 37 additions and 0 deletions

View file

@ -82,6 +82,10 @@ static int CmdHFEPACollectPACENonces(const char *Cmd) {
size_t nonce_length = resp.oldarg[1]; size_t nonce_length = resp.oldarg[1];
size_t nonce_length_bytes = 2 * nonce_length + 1; size_t nonce_length_bytes = 2 * nonce_length + 1;
char *nonce = (char *) calloc(2 * nonce_length + 1, sizeof(uint8_t)); char *nonce = (char *) calloc(2 * nonce_length + 1, sizeof(uint8_t));
if (nonce == NULL) {
PrintAndLogEx(FAILED, "Memory allocation failed for nonce");
return PM3_EMALLOC;
}
for (int j = 0; j < nonce_length; j++) { for (int j = 0; j < nonce_length; j++) {
int nonce_offset = 2 * j; int nonce_offset = 2 * j;
snprintf(nonce + nonce_offset, (nonce_length_bytes * sizeof(uint8_t)) - nonce_offset, "%02X", resp.data.asBytes[j]); snprintf(nonce + nonce_offset, (nonce_length_bytes * sizeof(uint8_t)) - nonce_offset, "%02X", resp.data.asBytes[j]);

View file

@ -114,6 +114,10 @@ static int CreateGetVASDataCommand(const uint8_t *pidHash, const char *url, size
size_t reqTlvLen = 19 + (pidHash != NULL ? 35 : 0) + (url != NULL ? 3 + urlLen : 0); size_t reqTlvLen = 19 + (pidHash != NULL ? 35 : 0) + (url != NULL ? 3 + urlLen : 0);
uint8_t *reqTlv = calloc(reqTlvLen, sizeof(uint8_t)); uint8_t *reqTlv = calloc(reqTlvLen, sizeof(uint8_t));
if (reqTlv == NULL) {
PrintAndLogEx(FAILED, "Memory allocation failed");
return PM3_EMALLOC;
}
uint8_t version[] = {0x9F, 0x22, 0x02, 0x01, 0x00}; uint8_t version[] = {0x9F, 0x22, 0x02, 0x01, 0x00};
memcpy(reqTlv, version, sizeof(version)); memcpy(reqTlv, version, sizeof(version));

View file

@ -291,6 +291,10 @@ static int CmdGuardClone(const char *Cmd) {
//GuardProxII - compat mode, ASK/Biphase, data rate 64, 3 data blocks //GuardProxII - compat mode, ASK/Biphase, data rate 64, 3 data blocks
uint8_t *bs = calloc(96, sizeof(uint8_t)); uint8_t *bs = calloc(96, sizeof(uint8_t));
if (bs == NULL) {
PrintAndLogEx(ERR, "Memory allocation failed.");
return PM3_EMALLOC;
}
if (getGuardBits(xorval, fmtlen, facilitycode, cardnumber, bs) != PM3_SUCCESS) { if (getGuardBits(xorval, fmtlen, facilitycode, cardnumber, bs) != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Error with tag bitstream generation."); PrintAndLogEx(ERR, "Error with tag bitstream generation.");
free(bs); free(bs);

View file

@ -199,6 +199,10 @@ static int CmdNoralsyClone(const char *Cmd) {
} }
uint8_t *bits = calloc(96, sizeof(uint8_t)); uint8_t *bits = calloc(96, sizeof(uint8_t));
if (bits == NULL) {
PrintAndLogEx(ERR, "Memory allocation failed.");
return PM3_EMALLOC;
}
if (getnoralsyBits(id, year, bits) != PM3_SUCCESS) { if (getnoralsyBits(id, year, bits) != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Error with tag bitstream generation."); PrintAndLogEx(ERR, "Error with tag bitstream generation.");
free(bits); free(bits);

View file

@ -182,6 +182,10 @@ static int cda_test_raw(bool verbose) {
size_t ipk_pk_len = ipk_data[13]; size_t ipk_pk_len = ipk_data[13];
unsigned char *ipk_pk = calloc(1, ipk_pk_len); unsigned char *ipk_pk = calloc(1, ipk_pk_len);
if (!ipk_pk) {
free(ipk_data);
return 1;
}
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36); memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
memcpy(ipk_pk + ipk_data_len - 36, c_issuer_rem, sizeof(c_issuer_rem)); memcpy(ipk_pk + ipk_data_len - 36, c_issuer_rem, sizeof(c_issuer_rem));

View file

@ -170,6 +170,10 @@ static int dda_test_raw(bool verbose) {
size_t ipk_pk_len = ipk_data[13]; size_t ipk_pk_len = ipk_data[13];
unsigned char *ipk_pk = calloc(1, ipk_pk_len); unsigned char *ipk_pk = calloc(1, ipk_pk_len);
if (!ipk_pk) {
free(ipk_data);
return 1;
}
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36); memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
memcpy(ipk_pk + ipk_data_len - 36, d_issuer_rem, sizeof(d_issuer_rem)); memcpy(ipk_pk + ipk_data_len - 36, d_issuer_rem, sizeof(d_issuer_rem));

View file

@ -132,6 +132,10 @@ static int sda_test_raw(bool verbose) {
size_t ipk_pk_len = ipk_data[13]; size_t ipk_pk_len = ipk_data[13];
unsigned char *ipk_pk = calloc(1, ipk_pk_len); unsigned char *ipk_pk = calloc(1, ipk_pk_len);
if (!ipk_pk) {
free(ipk_data);
return 1;
}
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36); memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
memcpy(ipk_pk + ipk_data_len - 36, issuer_rem, sizeof(issuer_rem)); memcpy(ipk_pk + ipk_data_len - 36, issuer_rem, sizeof(issuer_rem));

View file

@ -715,6 +715,10 @@ void print_progress(uint64_t count, uint64_t max, barMode_t style) {
size_t unit = strlen(block[mode]); size_t unit = strlen(block[mode]);
// +1 for \0 // +1 for \0
char *bar = (char *)calloc(unit * width + 1, sizeof(uint8_t)); char *bar = (char *)calloc(unit * width + 1, sizeof(uint8_t));
if (bar == NULL) {
fprintf(stderr, "Memory allocation failed for progress bar\n");
return;
}
uint8_t value = PERCENTAGE(count, max); uint8_t value = PERCENTAGE(count, max);
@ -739,6 +743,11 @@ void print_progress(uint64_t count, uint64_t max, barMode_t style) {
// color buffer // color buffer
size_t collen = strlen(bar) + 40; size_t collen = strlen(bar) + 40;
char *cbar = (char *)calloc(collen, sizeof(uint8_t)); char *cbar = (char *)calloc(collen, sizeof(uint8_t));
if (cbar == NULL) {
fprintf(stderr, "Memory allocation failed for color buffer\n");
free(bar);
return;
}
// Add colors // Add colors
if (g_session.supports_colors) { if (g_session.supports_colors) {