From a5759c572ed4948c660a06430b074bbc913fafc6 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 7 Dec 2014 02:58:24 +0100 Subject: [PATCH] Check return value of malloc --- src/ecdsa_impl.h | 2 +- src/ecmult_gen_impl.h | 2 +- src/ecmult_impl.h | 4 ++-- src/field_impl.h | 2 +- src/group_impl.h | 6 +++--- src/scalar_impl.h | 2 +- src/util.h | 6 ++++++ 7 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ecdsa_impl.h b/src/ecdsa_impl.h index a951d0b4ad..b76e98e4e3 100644 --- a/src/ecdsa_impl.h +++ b/src/ecdsa_impl.h @@ -27,7 +27,7 @@ static void secp256k1_ecdsa_start(void) { return; /* Allocate. */ - secp256k1_ecdsa_consts_t *ret = (secp256k1_ecdsa_consts_t*)malloc(sizeof(secp256k1_ecdsa_consts_t)); + secp256k1_ecdsa_consts_t *ret = (secp256k1_ecdsa_consts_t*)checked_malloc(sizeof(secp256k1_ecdsa_consts_t)); static const unsigned char order[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, diff --git a/src/ecmult_gen_impl.h b/src/ecmult_gen_impl.h index af0ead522d..5e291a0e78 100644 --- a/src/ecmult_gen_impl.h +++ b/src/ecmult_gen_impl.h @@ -34,7 +34,7 @@ static void secp256k1_ecmult_gen_start(void) { return; /* Allocate the precomputation table. */ - secp256k1_ecmult_gen_consts_t *ret = (secp256k1_ecmult_gen_consts_t*)malloc(sizeof(secp256k1_ecmult_gen_consts_t)); + secp256k1_ecmult_gen_consts_t *ret = (secp256k1_ecmult_gen_consts_t*)checked_malloc(sizeof(secp256k1_ecmult_gen_consts_t)); /* get the generator */ const secp256k1_ge_t *g = &secp256k1_ge_consts->g; diff --git a/src/ecmult_impl.h b/src/ecmult_impl.h index 690d525178..fe0b160dcd 100644 --- a/src/ecmult_impl.h +++ b/src/ecmult_impl.h @@ -43,7 +43,7 @@ static void secp256k1_ecmult_table_precomp_gej_var(secp256k1_gej_t *pre, const s static void secp256k1_ecmult_table_precomp_ge_var(secp256k1_ge_t *pre, const secp256k1_gej_t *a, int w) { const int table_size = 1 << (w-2); - secp256k1_gej_t *prej = malloc(sizeof(secp256k1_gej_t) * table_size); + secp256k1_gej_t *prej = checked_malloc(sizeof(secp256k1_gej_t) * table_size); prej[0] = *a; secp256k1_gej_t d; secp256k1_gej_double_var(&d, a); for (int i=1; ig; diff --git a/src/field_impl.h b/src/field_impl.h index 9ef8fb30c3..b23ba08d71 100644 --- a/src/field_impl.h +++ b/src/field_impl.h @@ -253,7 +253,7 @@ static void secp256k1_fe_start(void) { #endif if (secp256k1_fe_consts == NULL) { secp256k1_fe_inner_start(); - secp256k1_fe_consts_t *ret = (secp256k1_fe_consts_t*)malloc(sizeof(secp256k1_fe_consts_t)); + secp256k1_fe_consts_t *ret = (secp256k1_fe_consts_t*)checked_malloc(sizeof(secp256k1_fe_consts_t)); #ifndef USE_NUM_NONE secp256k1_num_set_bin(&ret->p, secp256k1_fe_consts_p, sizeof(secp256k1_fe_consts_p)); #endif diff --git a/src/group_impl.h b/src/group_impl.h index a8d0cb2682..97bd302952 100644 --- a/src/group_impl.h +++ b/src/group_impl.h @@ -85,14 +85,14 @@ static void secp256k1_ge_set_gej_var(secp256k1_ge_t *r, secp256k1_gej_t *a) { static void secp256k1_ge_set_all_gej_var(size_t len, secp256k1_ge_t r[len], const secp256k1_gej_t a[len]) { size_t count = 0; - secp256k1_fe_t *az = malloc(sizeof(secp256k1_fe_t) * len); + secp256k1_fe_t *az = checked_malloc(sizeof(secp256k1_fe_t) * len); for (size_t i=0; ibeta, secp256k1_ge_consts_beta)); #endif diff --git a/src/scalar_impl.h b/src/scalar_impl.h index 7fc159df77..4408cce2d8 100644 --- a/src/scalar_impl.h +++ b/src/scalar_impl.h @@ -40,7 +40,7 @@ static void secp256k1_scalar_start(void) { return; /* Allocate. */ - secp256k1_scalar_consts_t *ret = (secp256k1_scalar_consts_t*)malloc(sizeof(secp256k1_scalar_consts_t)); + secp256k1_scalar_consts_t *ret = (secp256k1_scalar_consts_t*)checked_malloc(sizeof(secp256k1_scalar_consts_t)); #ifndef USE_NUM_NONE static const unsigned char secp256k1_scalar_consts_order[] = { diff --git a/src/util.h b/src/util.h index 08b23a9d38..c3a8f3a42b 100644 --- a/src/util.h +++ b/src/util.h @@ -61,6 +61,12 @@ #define VERIFY_CHECK(cond) do { (void)(cond); } while(0) #endif +static inline void *checked_malloc(size_t size) { + void *ret = malloc(size); + CHECK(ret != NULL); + return ret; +} + /* Macro for restrict, when available and not in a VERIFY build. */ #if defined(SECP256K1_BUILD) && defined(VERIFY) # define SECP256K1_RESTRICT