use calloc instead

This commit is contained in:
iceman1001 2022-01-09 00:33:33 +01:00
commit fe9348768d
22 changed files with 70 additions and 69 deletions

View file

@ -63,7 +63,7 @@ static struct crypto_hash *crypto_hash_polarssl_open(enum crypto_algo_hash hash)
if (hash != HASH_SHA_1)
return NULL;
struct crypto_hash_polarssl *ch = malloc(sizeof(*ch));
struct crypto_hash_polarssl *ch = calloc(1, sizeof(*ch));
mbedtls_sha1_starts(&(ch->ctx));
@ -81,7 +81,7 @@ struct crypto_pk_polarssl {
};
static struct crypto_pk *crypto_pk_polarssl_open_rsa(va_list vl) {
struct crypto_pk_polarssl *cp = malloc(sizeof(*cp));
struct crypto_pk_polarssl *cp = calloc(1, sizeof(*cp));
memset(cp, 0x00, sizeof(*cp));
char *mod = va_arg(vl, char *); // N
@ -106,7 +106,7 @@ static struct crypto_pk *crypto_pk_polarssl_open_rsa(va_list vl) {
}
static struct crypto_pk *crypto_pk_polarssl_open_priv_rsa(va_list vl) {
struct crypto_pk_polarssl *cp = malloc(sizeof(*cp));
struct crypto_pk_polarssl *cp = calloc(1, sizeof(*cp));
memset(cp, 0x00, sizeof(*cp));
char *mod = va_arg(vl, char *);
int modlen = va_arg(vl, size_t);
@ -166,7 +166,7 @@ static int myrand(void *rng_state, unsigned char *output, size_t len) {
}
static struct crypto_pk *crypto_pk_polarssl_genkey_rsa(va_list vl) {
struct crypto_pk_polarssl *cp = malloc(sizeof(*cp));
struct crypto_pk_polarssl *cp = calloc(1, sizeof(*cp));
memset(cp, 0x00, sizeof(*cp));
int transient = va_arg(vl, int);
@ -198,7 +198,7 @@ static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk *_cp, co
*clen = 0;
size_t keylen = mbedtls_mpi_size(&cp->ctx.N);
unsigned char *result = malloc(keylen);
unsigned char *result = calloc(1, keylen);
if (!result) {
PrintAndLogEx(WARNING, "RSA encrypt failed. Can't allocate result memory");
return NULL;
@ -220,7 +220,7 @@ static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk *_cp, co
*clen = 0;
size_t keylen = mbedtls_mpi_size(&cp->ctx.N);
unsigned char *result = malloc(keylen);
unsigned char *result = calloc(1, keylen);
if (!result) {
PrintAndLogEx(WARNING, "RSA encrypt failed. Can't allocate result memory");
return NULL;
@ -250,7 +250,7 @@ static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk *_
// mod
case 0:
*plen = mbedtls_mpi_size(&cp->ctx.N);
result = malloc(*plen);
result = calloc(1, *plen);
memset(result, 0x00, *plen);
res = mbedtls_mpi_write_binary(&cp->ctx.N, result, *plen);
if (res < 0) {
@ -262,7 +262,7 @@ static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk *_
// exp
case 1:
*plen = mbedtls_mpi_size(&cp->ctx.E);
result = malloc(*plen);
result = calloc(1, *plen);
memset(result, 0x00, *plen);
res = mbedtls_mpi_write_binary(&cp->ctx.E, result, *plen);
if (res < 0) {

View file

@ -54,7 +54,7 @@ static size_t dol_calculate_len(const struct tlv *tlv, size_t data_len) {
struct tlv *dol_process(const struct tlv *tlv, const struct tlvdb *tlvdb, tlv_tag_t tag) {
size_t res_len;
if (!tlv || !(res_len = dol_calculate_len(tlv, 0))) {
struct tlv *res_tlv = malloc(sizeof(*res_tlv));
struct tlv *res_tlv = calloc(1, sizeof(*res_tlv));
res_tlv->tag = tag;
res_tlv->len = 0;
@ -63,7 +63,7 @@ struct tlv *dol_process(const struct tlv *tlv, const struct tlvdb *tlvdb, tlv_ta
return res_tlv;
}
struct tlv *res_tlv = malloc(sizeof(*res_tlv) + res_len);
struct tlv *res_tlv = calloc(1, sizeof(*res_tlv) + res_len);
if (!res_tlv)
return NULL;

View file

@ -202,7 +202,7 @@ struct emv_pk *emv_pk_parse_pk(char *buf, size_t buflen) {
goto out;
buf += l;
r->modulus = malloc(2048 / 8);
r->modulus = calloc(1, (2048 / 8));
l = emv_pk_read_bin(buf, buflen, r->modulus, 2048 / 8, &r->mlen);
if (l <= 0)
goto out2;
@ -267,8 +267,8 @@ static size_t emv_pk_write_str(char *out, size_t outlen, const char *str) {
char *emv_pk_dump_pk(const struct emv_pk *pk) {
size_t outpos = 0;
size_t outsize = 1024; /* should be enough */
char *out = malloc(outsize); /* should be enough */
size_t outsize = 1024; // should be enough
char *out = calloc(1, outsize); // should be enough
if (!out)
return NULL;

View file

@ -310,7 +310,7 @@ struct emv_pk *emv_pki_recover_icc_cert(const struct emv_pk *pk, struct tlvdb *d
sda_tlv,
&sda_tdata);
free(sdatl); // malloc here: emv_pki_sdatl_fill
free(sdatl); // calloc here: emv_pki_sdatl_fill
return res;
}
@ -345,7 +345,7 @@ unsigned char *emv_pki_sdatl_fill(const struct tlvdb *db, size_t *sdatl_len) {
if (len) {
*sdatl_len = len;
unsigned char *value = malloc(len);
unsigned char *value = calloc(1, len);
memcpy(value, buf, len);
return value;
}
@ -372,7 +372,7 @@ struct tlvdb *emv_pki_recover_dac_ex(const struct emv_pk *enc_pk, const struct t
&sda_tdata,
(uint8_t *)NULL);
free(sdatl); // malloc here: emv_pki_sdatl_fill
free(sdatl); // calloc here: emv_pki_sdatl_fill
if (!data || data_len < 5)
return NULL;

View file

@ -90,15 +90,15 @@ static struct tlvdb *emv_pki_sign_message(const struct crypto_pk *cp,
... /* A list of tlv pointers, end with NULL */
) {
size_t tmp_len = (crypto_pk_get_nbits(cp) + 7) / 8;
unsigned char *tmp = malloc(tmp_len);
if (!tmp)
unsigned char *tmp = calloc(1, tmp_len);
if (!tmp) {
return NULL;
}
// XXX
struct crypto_hash *ch = crypto_hash_open(HASH_SHA_1);
if (!ch) {
free(tmp);
return NULL;
}
@ -179,7 +179,7 @@ static struct tlvdb *emv_pki_sign_key(const struct crypto_pk *cp,
const struct tlv *add_tlv
) {
unsigned pos = 0;
unsigned char *msg = malloc(1 + pan_len + 2 + 3 + 1 + 1 + 1 + 1 + ipk->mlen);
unsigned char *msg = calloc(1, 1 + pan_len + 2 + 3 + 1 + 1 + 1 + 1 + ipk->mlen);
if (!msg)
return NULL;
@ -235,7 +235,7 @@ struct tlvdb *emv_pki_sign_icc_pe_cert(const struct crypto_pk *cp, struct emv_pk
struct tlvdb *emv_pki_sign_dac(const struct crypto_pk *cp, const struct tlv *dac_tlv, const struct tlv *sda_tlv) {
unsigned pos = 0;
unsigned char *msg = malloc(1 + 1 + dac_tlv->len);
unsigned char *msg = calloc(1, 1 + 1 + dac_tlv->len);
if (!msg)
return NULL;
@ -258,7 +258,7 @@ struct tlvdb *emv_pki_sign_dac(const struct crypto_pk *cp, const struct tlv *dac
struct tlvdb *emv_pki_sign_idn(const struct crypto_pk *cp, const struct tlv *idn_tlv, const struct tlv *dyn_tlv) {
unsigned pos = 0;
unsigned char *msg = malloc(1 + 1 + 1 + 1 + idn_tlv->len);
unsigned char *msg = calloc(1, 1 + 1 + 1 + 1 + idn_tlv->len);
if (!msg)
return NULL;

View file

@ -23,7 +23,7 @@
#endif
#include <string.h> // memcpy
#include <stdlib.h> // malloc
#include <stdlib.h> // calloc
#include "cda_test.h"
#include "../emv_pk.h"
@ -181,7 +181,7 @@ static int cda_test_raw(bool verbose) {
}
size_t ipk_pk_len = ipk_data[13];
unsigned char *ipk_pk = malloc(ipk_pk_len);
unsigned char *ipk_pk = calloc(1, ipk_pk_len);
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
memcpy(ipk_pk + ipk_data_len - 36, c_issuer_rem, sizeof(c_issuer_rem));
@ -240,7 +240,7 @@ static int cda_test_raw(bool verbose) {
}
size_t iccpk_pk_len = iccpk_data[19];
unsigned char *iccpk_pk = malloc(iccpk_pk_len);
unsigned char *iccpk_pk = calloc(1, iccpk_pk_len);
memcpy(iccpk_pk, iccpk_data + 21, /*iccpk_data_len - 36*/iccpk_pk_len);
/*memcpy(iccpk_pk + iccpk_data_len - 36, icc_rem, sizeof(icc_rem));*/

View file

@ -23,7 +23,7 @@
#endif
#include <string.h> // memcpy
#include <stdlib.h> // malloc
#include <stdlib.h> // calloc
#include <inttypes.h>
#include "crypto_test.h"

View file

@ -23,7 +23,7 @@
#endif
#include <string.h> // memcpy
#include <stdlib.h> // malloc
#include <stdlib.h> // calloc
#include "dda_test.h"
#include "../emv_pk.h"
@ -169,7 +169,7 @@ static int dda_test_raw(bool verbose) {
}
size_t ipk_pk_len = ipk_data[13];
unsigned char *ipk_pk = malloc(ipk_pk_len);
unsigned char *ipk_pk = calloc(1, ipk_pk_len);
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
memcpy(ipk_pk + ipk_data_len - 36, d_issuer_rem, sizeof(d_issuer_rem));
@ -228,7 +228,7 @@ static int dda_test_raw(bool verbose) {
}
size_t iccpk_pk_len = iccpk_data[19];
unsigned char *iccpk_pk = malloc(iccpk_pk_len);
unsigned char *iccpk_pk = calloc(1, iccpk_pk_len);
memcpy(iccpk_pk, iccpk_data + 21, /*iccpk_data_len - 36*/iccpk_pk_len);
/*memcpy(iccpk_pk + iccpk_data_len - 36, icc_rem, sizeof(icc_rem));*/

View file

@ -131,7 +131,7 @@ static int sda_test_raw(bool verbose) {
}
size_t ipk_pk_len = ipk_data[13];
unsigned char *ipk_pk = malloc(ipk_pk_len);
unsigned char *ipk_pk = calloc(1, ipk_pk_len);
memcpy(ipk_pk, ipk_data + 15, ipk_data_len - 36);
memcpy(ipk_pk + ipk_data_len - 36, issuer_rem, sizeof(issuer_rem));

View file

@ -26,6 +26,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define TLV_TAG_CLASS_MASK 0xc0
#define TLV_TAG_COMPLEX 0x20
@ -163,7 +164,7 @@ static struct tlvdb *tlvdb_parse_children(struct tlvdb *parent) {
struct tlvdb *tlvdb, *first = NULL, *prev = NULL;
while (left != 0) {
tlvdb = malloc(sizeof(*tlvdb));
tlvdb = calloc(1, sizeof(*tlvdb));
if (prev)
prev->next = tlvdb;
else
@ -192,7 +193,7 @@ struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len) {
if (!len || !buf)
return NULL;
root = malloc(sizeof(*root) + len);
root = calloc(1, sizeof(*root) + len);
root->len = len;
memcpy(root->buf, buf, len);
@ -221,7 +222,7 @@ struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len) {
if (!len || !buf)
return NULL;
root = malloc(sizeof(*root) + len);
root = calloc(1, sizeof(*root) + len);
root->len = len;
memcpy(root->buf, buf, len);
@ -232,7 +233,7 @@ struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len) {
goto err;
while (left != 0) {
struct tlvdb *db = malloc(sizeof(*db));
struct tlvdb *db = calloc(1, sizeof(*db));
if (!tlvdb_parse_one(db, NULL, &tmp, &left)) {
free(db);
goto err;
@ -250,7 +251,7 @@ err:
}
struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value) {
struct tlvdb_root *root = malloc(sizeof(*root) + len);
struct tlvdb_root *root = calloc(1, sizeof(*root) + len);
root->len = len;
memcpy(root->buf, value, len);
@ -264,7 +265,7 @@ struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value)
}
struct tlvdb *tlvdb_external(tlv_tag_t tag, size_t len, const unsigned char *value) {
struct tlvdb_root *root = malloc(sizeof(*root));
struct tlvdb_root *root = calloc(1, sizeof(*root));
root->len = 0;
@ -489,7 +490,7 @@ unsigned char *tlv_encode(const struct tlv *tlv, size_t *len) {
else
size += 1;
data = malloc(size);
data = calloc(1, size);
if (!data) {
*len = 0;
return NULL;

View file

@ -1912,7 +1912,7 @@ SWIGRUNTIME PyObject *
SwigPyPacked_New(void *ptr, size_t size, swig_type_info *ty) {
SwigPyPacked *sobj = PyObject_NEW(SwigPyPacked, SwigPyPacked_type());
if (sobj) {
void *pack = malloc(size);
void *pack = calloc(1, size);
if (pack) {
memcpy(pack, ptr, size);
sobj->pack = pack;

View file

@ -82,7 +82,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
char *prefix = strdup(pcPortName);
if (prefix == NULL) {
PrintAndLogEx(ERR, "error: malloc");
PrintAndLogEx(ERR, "error: string duplication");
free(sp);
return INVALID_SERIAL_PORT;
}
@ -100,7 +100,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
char *addrstr = strdup(pcPortName + 4);
if (addrstr == NULL) {
PrintAndLogEx(ERR, "error: malloc");
PrintAndLogEx(ERR, "error: string duplication");
free(sp);
return INVALID_SERIAL_PORT;
}
@ -175,7 +175,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
char *addrstr = strndup(pcPortName + 3, 17);
if (addrstr == NULL) {
PrintAndLogEx(ERR, "error: malloc");
PrintAndLogEx(ERR, "error: string duplication");
free(sp);
return INVALID_SERIAL_PORT;
}